Internationalization Collator::compare

Collator::compare

collator_compare

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

Collator::compare -- collator_compare — 두 유니코드 문자열 비교


설명

객체 지향 스타일

public Collator::compare(string $string1, string $string2): bool

절차적 스타일

collator_compare(Collator $object, string $string1, string $string2): int|false

조합 규칙에 따라 두 개의 유니코드 문자열을 비교합니다.


매개변수

object
Collator object.
string1
비교할 첫 번째 문자열입니다.
string2
비교할 두 번째 문자열입니다.

반환 값

비교 결과 반환:

  • string1string2보다 큰 경우 1 ;
  • string1string2와 같으면 0이고;
  • string1string2보다 작은 경우 -1입니다.

실패 시 false를 반환합니다.

경고 이 함수는 부울 false을 반환할 수 있지만 false으로 평가되는 부울이 아닌 값을 반환할 수도 있습니다. 자세한 내용은 부울 섹션을 참조하세요. 이 함수의 반환 값을 테스트하려면 === 연산자를 사용하십시오.


Examples

예제 #1 collator_compare() 예제

                  
<?php
$s1 = 'Hello';
$s2 = 'hello';

$coll = collator_create( 'en_US' );
$res  = collator_compare( $coll, $s1, $s2 );

if ($res === false) {
    echo collator_get_error_message( $coll );
} else if( $res > 0 ) {
    echo "s1 is greater than s2\n";
} else if( $res < 0 ) {
    echo "s1 is less than s2\n";
} else {
    echo "s1 is equal to s2\n";
}
?>
                  
                

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

s1 is greater than s2
                

예제 #2 분음 부호 또는 대소문자 구분 없이 문자열 비교

                  
<?php
$c = new Collator( 'en' );
$c->setStrength( Collator::PRIMARY );

if ( $c->compare( 'Séan', 'Sean' ) == 0 )
{
    echo "The same\n";
}
                  
                

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

The same
                

이 예제는 기본 문자만 고려하여 비교하도록 collator에 지시합니다. Collator->setStrength()에 대한 문서는 다양한 장점을 설명합니다.


기타