note 125616 deleted from function.natcasesort by cmb
| From: | cmb@php.net | Date: | Sat, 19 Dec 2020 15:31:41 +0000 |
| Subject: | note 125616 deleted from function.natcasesort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216003@lists.php.net to get a copy of this message | ||
Note Submitter: goodwaiter at gmail dot com
----
if you want natcasesort() + sort() by local (eg. utf-8 chinese) combine like windows system
does(number and a-zA-Z ascii first, and then chinese sort by pinyin, or other language sort by
pronunciation), read this:
function natcasesort_local($a, $local = "", $reg_pre = "^", $reg_end =
"")
{
//eg.: $local = "zh-CN.UTF-8", or other, more in setlocale()
//$reg_pre, $reg_end need to be regex, eg.: exclude dirs: $reg_pre = "[\/\\\\]", $reg_end
= "[^\/\\\\]*$"
natcasesort($a);
end($a);
$temp_a = array();
for ( $slice_i=0; $slice_i<=count($a); $slice_i++ )
{
if ( preg_match("/" . $reg_pre . "[^\x01-\x7F]" . $reg_end . "/isu",
current($a)) )
{
prev($a);
} else
{
if ( 0 < $slice_i )
{
$temp_a = array_slice($a, -1*$slice_i);
setlocale(LC_ALL, $local);
sort($temp_a,SORT_LOCALE_STRING);
}
break;
}
}
array_splice($a, -1*$slice_i, $slice_i, $temp_a);
return $a;
}
eg.:
$a_basename_or_string = array("1.jpg", "12.jpg", "2.jpg",
"ä».jpg", "å¬.jpg");
$a_filename_with_dir = array("E:/temp/1.jpg", "E:/temp/12.jpg",
"E:/temp/2.jpg", "E:/temp/ä».jpg", "E:/temp/å¬.jpg");
print_r(natcasesort_local($a_basename_or_string, "zh-CN.UTF-8"));//right
print_r(natcasesort_local($a_filename_with_dir, "zh-CN.UTF-8"));//wrong
print_r(natcasesort_local($a_filename_with_dir, "zh-CN.UTF-8", "[\/\\\\]",
"[^\/\\\\]*$"));//right
print_r(natcasesort_local($a_filename_with_dir, "zh-CN.UTF-8", "E:\/temp\/",
""));//right
/*
Array
(
[0] => 1.jpg
[1] => 2.jpg
[2] => 12.jpg
[3] => å¬.jpg
[4] => ä».jpg
)
Array
(
[0] => E:/temp/1.jpg
[1] => E:/temp/2.jpg
[2] => E:/temp/12.jpg
[3] => E:/temp/ä».jpg
[4] => E:/temp/å¬.jpg
)
Array
(
[0] => E:/temp/1.jpg
[1] => E:/temp/2.jpg
[2] => E:/temp/12.jpg
[3] => E:/temp/å¬.jpg
[4] => E:/temp/ä».jpg
)
Array
(
[0] => E:/temp/1.jpg
[1] => E:/temp/2.jpg
[2] => E:/temp/12.jpg
[3] => E:/temp/å¬.jpg
[4] => E:/temp/ä».jpg
)
*/