Strings str_contains

(PHP 8)

str_contains — 문자열에 주어진 부분 문자열이 포함되어 있는지 확인


설명

str_contains(string $haystack, string $needle): bool

haystackneedle이 포함되어 있는지 여부를 나타내는 대소문자를 구분하는 검사를 수행합니다.


매개변수

haystack
검색할 문자열입니다.
needle
haystack에서 검색할 하위 문자열입니다.

반환 값

needlehaystack에 있으면 true를 반환하고 그렇지 않으면 false를 반환합니다.


Examples

예제 #1 빈 문자열 '' 사용

                  
<?php
if (str_contains('abc', '')) {
    echo "Checking the existence of the empty string will always return true";
}
?>
                  
                

위의 예는 다음을 출력합니다.

Checking the existence of the empty string will always return true
                

예제 #2 대소문자 구분 표시

                  
<?php
$string = 'The lazy fox jumped over the fence';

if (str_contains($string, 'lazy')) {
    echo "The string 'lazy' was found in the string\n";
}

if (str_contains($string, 'Lazy')) {
    echo 'The string "Lazy" was found in the string';
} else {
    echo '"Lazy" was not found because the case does not match';
}

?>
                  
                

위의 예는 다음을 출력합니다.

The string 'lazy' was found in the string
"Lazy" was not found because the case does not match
                

메모

참고: 이 함수는 binary-safe입니다.


기타

  • str_ends_with() - 문자열이 주어진 부분 문자열로 끝나는지 확인
  • str_starts_with() - 문자열이 주어진 부분 문자열로 시작하는지 확인
  • stripos() - 문자열에서 대소문자를 구분하지 않는 하위 문자열이 처음 나타나는 위치 찾기
  • strrpos() - 문자열에서 하위 문자열이 마지막으로 나타나는 위치 찾기
  • strripos() - 문자열에서 대소문자를 구분하지 않는 하위 문자열이 마지막으로 나타나는 위치 찾기
  • strstr() - 문자열의 첫 번째 항목 찾기
  • strpbrk() - 임의의 문자 집합에 대한 문자열 검색
  • substr() - 문자열의 일부 반환
  • preg_match() - 정규식 일치 수행