Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : strcoll

strcoll

Locale based string comparison (PHP 4 >= 4.0.5, PHP 5)
int strcoll ( string str1, string str2 )


Code Examples / Notes » strcoll

sakkarinlaohawisut15

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
)


27-aug-2002 03: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!


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker