There seems to be a bug in the localization for strnatcmp and strnatcasecmp. I searched the reported bugs and found a few entries which were up to four years old (but the problem still exists when using swedish characters).
These functions might work instead.
<?php
function _strnatcasecmp($left, $right) {
return _strnatcmp(strtolower($left), strtolower($right));
}
function _strnatcmp($left, $right) {
while((strlen($left) > 0) && (strlen($right) > 0)) {
if(preg_match('/^([^0-9]*)([0-9].*)$/Us', $left, $lMatch)) {
$lTest = $lMatch[1];
$left = $lMatch[2];
} else {
$lTest = $left;
$left = '';
}
if(preg_match('/^([^0-9]*)([0-9].*)$/Us', $right, $rMatch)) {
$rTest = $rMatch[1];
$right = $rMatch[2];
} else {
$rTest = $right;
$right = '';
}
$test = strcmp($lTest, $rTest);
if($test != 0) {
return $test;
}
if(preg_match('/^([0-9]+)([^0-9].*)?$/Us', $left, $lMatch)) {
$lTest = intval($lMatch[1]);
$left = $lMatch[2];
} else {
$lTest = 0;
}
if(preg_match('/^([0-9]+)([^0-9].*)?$/Us', $right, $rMatch)) {
$rTest = intval($rMatch[1]);
$right = $rMatch[2];
} else {
$rTest = 0;
}
$test = $lTest - $rTest;
if($test != 0) {
return $test;
}
}
return strcmp($left, $right);
}
?>
The code is not optimized. It was just made to solve my problem.
strnatcmp
(PHP 4, PHP 5)
strnatcmp — "natural order" 알고리즘을 이용한 문자열 비교
설명
int strnatcmp
( string $str1
, string $str2
)
사람이 하는 것과 같은 방법으로 알파벳과 숫자로 이루어진 문자열의 비교 알고리즘, "natural ordering"을 수행합니다. 아래는 이 알고리즘과 컴퓨터가 사용하는 정렬 알고리즘(strcmp()에서 사용)의 차이입니다:
<?php
$arr1 = $arr2 = array("img12.png", "img10.png", "img2.png", "img1.png");
echo "표준 문자열 비교\n";
usort($arr1, "strcmp");
print_r($arr1);
echo "\nNatural order 문자열 비교\n";
usort($arr2, "strnatcmp");
print_r($arr2);
?>
표준 문자열 비교 Array ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png ) Natural order 문자열 비교 Array ( [0] => img1.png [1] => img2.png [2] => img10.png [3] => img12.png )
다른 문자열 비교 함수와 동일하게, str1 이 str2 보다 작으면 < 0을; str1 이 str2 보다 크면 > 0을, 같으면 0을 반환합니다.
이 비교는 대소문자를 구별함에 주의하십시오.
참고: ereg(), strcasecmp(), substr(), stristr(), strcmp(), strncmp(), strncasecmp(), strnatcasecmp(), strstr(), natsort(), natcasesort().
strnatcmp
thomas at uninet dot se
25-Jul-2006 12:50
25-Jul-2006 12:50
