곰시기's

[PHP] - 문자열 앞까지 자르기 본문

Web Development/PHP

[PHP] - 문자열 앞까지 자르기

곰시기 2021. 2. 16. 15:36
function contentEnd( $content, $string, $len_include = false ) {
  if( "5.3" <= phpversion() ) {
    $content = strchr( $content, $string, true );
    // PHP ver 5.3~ 세번째 인자 bool값 지정 가능
    // bool값 : ture  : 검색한 문자열의 앞에 존재 하는 string을 반환
    //        : false : default값으로 검색 문자 뒤의 string을 반환
  }
  else{
    $tmp = stripos( $content, $string ); // '$string'앞에서 자르기 위해 위치 탐색

    if( $len_include ) {
      $tmp += strlen( $string ); // $len_include 값이 true 면 검색 문자열 포함 false면 검색 문자열 앞 까지
    }
    $content = substr( $content, 0, $tmp );
  }

  return $content;
}

'Web Development > PHP' 카테고리의 다른 글

[PHP] - Select Box  (0) 2021.02.16
[PHP] - JsonEncoder 함수  (0) 2021.02.16
[PHP] - 간단한 iconv 함수  (0) 2021.02.16
[PHP] - 원격지 파일 다운로드  (0) 2021.02.16
[PHP] - mysql_free_result  (0) 2021.01.20
Comments