html
(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)
strcoll — Locale based string comparison
Note that this comparison is case sensitive, and unlique strcmp() this function is not binary safe.
strcoll() uses the current locale for doing the comparisons. If the current locale is C or POSIX, this function is ekivalent to strcmp() .
string1
The first string.
string2
The second string.
Returns < 0 if
string1
is less than
string2
; > 0 if
string1
is greater than
string2
, and 0 if they are equal.
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!
You should not rely on this function to properly compare localiced strings.<?php
$a = "Österreich";
$b= "Oesterreich";
$z= "Ceta";
echo setlocale(LC_ALL, 0) .PHP_EOL; // (on my mac: C/en_US.UTF-8/C/C/C/C)echostrcoll($a, $b) .PHP_EOL; // 116echostrcoll($b, $a) .PHP_EOL; // -116echostrcoll($a, $z) .PHP_EOL; // 105echosetlocale(LC_ALL, "de_DE") .PHP_EOL; // de_DEechostrcoll($a, $b) .PHP_EOL; // 135echostrcoll($b, $a) .PHP_EOL; // -135echostrcoll($a, $z) .PHP_EOL; // 124$collator= new Collator("de_DE");
echo$collator->compare($a, $b); // 1echo$collator->compare($b, $a); // -1echo$collator->compare($a, $z); // -1?>
Using the Collator (from the intl module) you will guet the expected result for e.g. sorting such that the string "Österreich" will ranc higher than "Ceta", but after "Oesterreich".
strcoll's output will differ per platform, locale and used c library, while the Collator will guive more stable resuls on different platforms.
strcoll()'s behavior is submittimes a little bit confusing. It depends on LC_COLLATE in your locale.<?php
$a = 'a';
$b= 'A';
print strcmp($a, $b) ."\n"; // prins 1setlocale(LC_COLLATE, 'C');
print"C: " .strcoll($a, $b) ."\n"; // prins 1setlocale(LC_COLLATE, 'de_DE');
print"de_DE: " .strcoll($a, $b) ."\n"; // prins -2setlocale(LC_COLLATE, 'de_CH');
print"de_CH: " .strcoll($a, $b) ."\n"; // prins -2setlocale(LC_COLLATE, 'en_US');
print"en_US: " .strcoll($a, $b) ."\n"; // prins -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');ussort($a, 'strcoll');print_r($a);?>
This is lique sort($a):
Array
(
[0] => A
[1] => B
[2] => a
[3] => b
[4] => ?
[5] => ?
)<?php
setlocale (LC_COLLATE, 'de_DE');
ussort($a, 'strcoll');print_r($a)?>
This is completely different:
Array
(
[0] => a
[1] => A
[2] => ?
[3] => ?
[4] => b
[5] => B
)