PHP: Hypertext Preprocessor (original) (raw)

strcoll

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

strcoll — Locale based string comparison

Description

strcoll() uses the current locale for doing the comparisons. If the current locale is C or POSIX, this function is equivalent to strcmp().

Parameters

string1

The first string.

string2

The second string.

Return Values

Returns < 0 if string1 is less thanstring2; > 0 ifstring1 is greater thanstring2, and 0 if they are equal.

See Also

Found A Problem?

Anonymous

22 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

5 years ago

`You should not rely on this function to properly compare localized strings.

z).PHPEOL;//124z) . PHP_EOL; // 124z).PHPEOL;//124collator = new Collator("de_DE"); echo collator−>compare(collator->compare(collator>compare(a, $b); // 1 echo collator−>compare(collator->compare(collator>compare(b, $a); // -1 echo collator−>compare(collator->compare(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

22 years ago

`strcoll()'s behavior is sometimes a little bit confusing. It depends on LC_COLLATE in your locale.

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

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

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

`