note 124090 added to function.strcoll
| From: | mkroese at eljakim dot nl | Date: | Fri, 02 Aug 2019 09:13:04 +0000 |
| Subject: | note 124090 added to function.strcoll | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-211772@lists.php.net to get a copy of this message | ||
You should not rely on this function to properly compare localized strings.
<?php
$a = "Ãsterreich";
$b = "Oesterreich";
$z = "Zeta";
echo 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");
echo $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.
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.195 (proxied: 2001:1c02:1001:c300:5554:6cb:178c:9f9e)
----
Manual Page -- http://php.net/manual/en/function.strcoll.php
Edit -- https://master.php.net/note/edit/124090
Del: integrated -- https://master.php.net/note/delete/124090/integrated
Del: useless -- https://master.php.net/note/delete/124090/useless
Del: bad code -- https://master.php.net/note/delete/124090/bad+code
Del: spam -- https://master.php.net/note/delete/124090/spam
Del: non-english -- https://master.php.net/note/delete/124090/non-english
Del: in docs -- https://master.php.net/note/delete/124090/in+docs
Del: other reasons-- https://master.php.net/note/delete/124090
Reject -- https://master.php.net/note/reject/124090
Search -- https://master.php.net/manage/user-notes.php