Strings substr_compare

(PHP 5, PHP 7, PHP 8)

substr_compare — 오프셋에서 최대 길이 문자까지 두 문자열의 이진 안전 비교


설명

substr_compare(
    string $haystack,
    string $needle,
    int $offset,
    ?int $length = null,
    bool $case_insensitive = false
): int
                

substr_compare()는 위치 offset에서 needlehaystack를 최대 length 문자까지 비교합니다.


매개변수

haystack
비교되는 기본 문자열입니다.
needle
비교 중인 보조 문자열입니다.
offset
비교의 시작 위치입니다. 음수이면 문자열 끝부터 계산을 시작합니다.
length
비교의 길이입니다. 기본값은 haystack의 길이에서 offset을 뺀 것과 비교한 needle 길이의 가장 큰 값입니다.
case_insensitive
case_insensitivetrue이면 비교는 대소문자를 구분하지 않습니다.

반환 값

위치 offsethaystackneedle보다 작으면 < 0, needle보다 크면 > 0, 같으면 0을 반환합니다. offset이 (PHP 7.2.18, 7.3.5 이전) haystack의 길이보다 크거나 같거나 length가 설정되고 0보다 작은 경우 substr_compare()는 경고를 출력하고 false를 반환합니다.


변경 로그

버전 설명
8.0.0 length는 이제 nullable입니다.
7.2.18, 7.3.5 offset은 이제 haystack의 길이와 같을 수 있습니다.

Examples

예제 #1 substr_compare() 예제

                  
<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "de", -2, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning
?>
                  
                

기타

  • strncmp() - 처음 n개의 문자에 대한 이진 안전 문자열 비교