字符串函数
在线手册:中文 英文
PHP手册

strcoll

(PHP 4 >= 4.0.5, PHP 5)

strcoll基于区域设置的字符串比较

说明

int strcoll ( string $str1 , string $str2 )

注意该比较区分大小写。和 strcmp() 不同,该函数不是二进制安全的。

strcoll() 使用当前区域设置进行比较。如果当前区域为 C 或 POSIX,该函数等同于 strcmp()

参数

str1

第一个字符串。

str2

第二个字符串。

返回值

如果 str1 小于 str2,返回负数;如果 str1 大于 str2,返回正数;二者相等则返回 0。

更新日志

版本 说明
4.2.3 函数在 Win32 平台可用。

参见


字符串函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 基于区域设置的字符串比较

用户评论:

sakkarinlaohawisut15 at hotmail dot com (22-Mar-2003 07:31)

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!