strcoll()'s behavior is sometimes a little bit confusing. It depends on LC_COLLATE in your locale.
<?php
$a = 'a';
$b = 'A';
print strcmp ($a, $b) . "\n"; // prints 1
setlocale (LC_COLLATE, 'C');
print "C: " . strcoll ($a, $b) . "\n"; // prints 1
setlocale (LC_COLLATE, 'de_DE');
print "de_DE: " . strcoll ($a, $b) . "\n"; // prints -2
setlocale (LC_COLLATE, 'de_CH');
print "de_CH: " . strcoll ($a, $b) . "\n"; // prints -2
setlocale (LC_COLLATE, 'en_US');
print "en_US: " . strcoll ($a, $b) . "\n"; // prints -2
?>
This is useful e. g. if want to sort an array by using strcoll:
<?php
$a = array ('a', 'A', '', '', 'b', 'B');
setlocale (LC_COLLATE, 'C');
usort ($a, 'strcoll');
print_r ($a);
?>
This is like sort($a):
Array
(
[0] => A
[1] => B
[2] => a
[3] => b
[4] =>
[5] =>
)
<?php
setlocale (LC_COLLATE, 'de_DE');
usort ($a, 'strcoll');
print_r ($a)
?>
This is completely different:
Array
(
[0] => a
[1] => A
[2] =>
[3] =>
[4] => b
[5] => B
)
strcoll
(PHP 4 >= 4.0.5, PHP 5)
strcoll — 로케일 기반 문자열 비교
설명
int strcoll
( string $str1
, string $str2
)
이 비교는 대소문자를 구분하며, strcmp()와는 다르게 바이너리 안전이 아닙니다.
strcoll()은 비교할 때 현재 로케일을 사용합니다. 현재 로케일이 C나 POSIX이면, 이 함수는 strcmp()와 동일합니다.
인수
- str1
-
첫번째 문자열
- str2
-
두번째 문자열
반환값
str1 이 str2 보다 작으면 < 0을; str1 이 str2 보다 크면 > 0을, 동일하면 0을 반환합니다.
변경점
| 버전 | 설명 |
|---|---|
| 4.2.3 | win32에서 작동합니다. |
참고
- preg_match() - 정규표현식 매치를 수행
- strcmp() - 바이너리 안전 문자열 비교
- strcasecmp() - 대소문자 구분 없는 바이너리 안전 문자열 비교
- substr() - Return part of a string
- stristr() - 대소문자를 구분하지 않는 strstr
- strncasecmp() - 대소문자 구분 없는 처음 n 문자의 바이너리 안전 문자열 비교
- strncmp() - 처음 n 문자의 바이너리 안전 문자열 비교
- strstr() - 문자열이 처음으로 나오는 부분을 찾습니다
- setlocale() - Set locale information
strcoll
sakkarinlaohawisut15 at hotmail dot com
22-Mar-2003 07:31
22-Mar-2003 07:31
27-Aug-2002 02:05
Note that some platforms implement strcmp() and strcasecmp() according to the current locale when strings are not binary equal, so that strcmp() and strcoll() will return the same value! This depends on how the PHP strcmp() function is compiled (i.e. if it uses the platform specific strcmp() found in its standard library!).
In that case, the only difference between strcoll() and strcmp() is that strcoll() may return 0 for distinct strings(i.e. consider strings are equal) while strcmp() will differentiate them if they have distinct binary encoding! This typically occurs on Asian systems.
What you can be sure is that strcmp() will always differentiate strings that are encoded differently, but the relative order may still use the current locale setting for collation order!
