strcoll

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

strcollロケールに基づく文字列比較

説明

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

この比較は大文字小文字を区別すること、そして、 strcmp() とは異なり、 バイナリセーフではないことに注意してください。

strcoll() は比較を行う際に現在のロケールを使用します。 ロケールが C または POSIX の場合、この関数は strcmp() と等価です。

パラメータ

string1

最初の文字列。

string2

次の文字列。

戻り値

string1string2 より小さい場合に 0 未満の値、string1string2 より大きい場合に 0 より大きな値、 両者が等しい場合に 0 を返します。

参考

  • preg_match() - 正規表現によるマッチングを行う
  • strcmp() - バイナリセーフな文字列比較
  • strcasecmp() - 大文字小文字を区別しないバイナリセーフな文字列比較を行う
  • substr() - 文字列の一部分を返す
  • stristr() - 大文字小文字を区別しない strstr
  • strncasecmp() - バイナリセーフで大文字小文字を区別しない文字列比較を、最初の n 文字について行う
  • strncmp() - 最初の n 文字についてバイナリセーフな文字列比較を行う
  • strstr() - 文字列が最初に現れる位置を見つける
  • setlocale() - ロケール情報を設定する

add a note add a note

User Contributed Notes 3 notes

up
6
mkroese at eljakim dot nl
5 years ago
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.
up
7
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!
up
-1
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.

<?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
)
To Top