CascadiaPHP 2024


(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)

strcollComparaison de chaînes localisées


strcoll(string $string1, string $string2): int

Notez que cette comparaison est sensible à la casse, et que, contrairement à strcmp(), elle n'est pas compatible avec les chaînes binaires.

strcoll() utilise les locales courantes pour effectuer la comparaison. Si la locale courante est C ou POSIX, cette fonction est alors équivalente à la fonction strcmp().

Liste de paramètres


La première chaîne.


La seconde chaîne.

Valeurs de retour

Retourne < 0 si string1 est inférieure à string2; > 0 si string1 est supérieure à string2, et 0 si les deux chaînes sont égales.

Voir aussi

  • preg_match() - Effectue une recherche de correspondance avec une expression rationnelle standard
  • strcmp() - Comparaison binaire de chaînes
  • strcasecmp() - Comparaison insensible à la casse de chaînes binaires
  • substr() - Retourne un segment de chaîne
  • stristr() - Version insensible à la casse de strstr
  • strncasecmp() - Compare en binaire des chaînes de caractères
  • strncmp() - Comparaison binaire des n premiers caractères
  • strstr() - Trouve la première occurrence dans une chaîne
  • setlocale() - Modifie les informations de localisation

add a note

User Contributed Notes 3 notes

21 years ago
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!
mkroese at eljakim dot nl
4 years ago
You should not rely on this function to properly compare localized strings.

= "Österreich";
$b = "Oesterreich";
$z = "Zeta";

setlocale(LC_ALL, 0) . PHP_EOL; // (on my mac: C/en_US.UTF-8/C/C/C/C)
echo strcoll($a, $b) . PHP_EOL; // 116
echo strcoll($b, $a) . PHP_EOL; // -116
echo strcoll($a, $z) . PHP_EOL; // 105

echo setlocale(LC_ALL, "de_DE") . PHP_EOL; // de_DE
echo strcoll($a, $b) . PHP_EOL; // 135
echo strcoll($b, $a) . PHP_EOL; // -135
echo strcoll($a, $z) . PHP_EOL; // 124

$collator = new Collator("de_DE");
$collator->compare($a, $b); // 1
echo $collator->compare($b, $a); // -1
echo $collator->compare($a, $z); // -1

Using the Collator (from the intl module) you will get the expected result for e.g. sorting such that the string "Österreich" will rank higher than "Zeta", but after "Oesterreich".

strcoll's output will differ per platform, locale and used c library, while the Collator will give more stable results on different platforms.
sakkarinlaohawisut15 at hotmail dot com
21 years ago
strcoll()'s behavior is sometimes a little bit confusing. It depends on LC_COLLATE in your locale.


= 'a';
$b = 'A';

strcmp ($a, $b) . "\n"; // prints 1

setlocale (LC_COLLATE, 'C');
"C: " . strcoll ($a, $b) . "\n"; // prints 1

setlocale (LC_COLLATE, 'de_DE');
"de_DE: " . strcoll ($a, $b) . "\n"; // prints -2

setlocale (LC_COLLATE, 'de_CH');
"de_CH: " . strcoll ($a, $b) . "\n"; // prints -2

setlocale (LC_COLLATE, 'en_US');
"en_US: " . strcoll ($a, $b) . "\n"; // prints -2


This is useful e. g. if want to sort an array by using strcoll:


= array ('a', 'A', '?', '?', 'b', 'B');

setlocale (LC_COLLATE, 'C');
usort ($a, 'strcoll');
print_r ($a);


This is like sort($a):
[0] => A
[1] => B
[2] => a
[3] => b
[4] => ?
[5] => ?


(LC_COLLATE, 'de_DE');
usort ($a, 'strcoll');
print_r ($a)


This is completely different:
[0] => a
[1] => A
[2] => ?
[3] => ?
[4] => b
[5] => B
To Top