note 125616 added to function.natcasesort

From: Date: Sat, 19 Dec 2020 15:08:49 +0000
Subject: note 125616 added to function.natcasesort
Groups: php.notes 
Request: Send a blank email to php-notes+get-215997@lists.php.net to get a copy of this message
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 ) */ ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.165 (proxied: 113.118.26.171) ---- Manual Page -- http://php.net/manual/en/function.natcasesort.php Edit -- https://master.php.net/note/edit/125616 Del: integrated -- https://master.php.net/note/delete/125616/integrated Del: useless -- https://master.php.net/note/delete/125616/useless Del: bad code -- https://master.php.net/note/delete/125616/bad+code Del: spam -- https://master.php.net/note/delete/125616/spam Del: non-english -- https://master.php.net/note/delete/125616/non-english Del: in docs -- https://master.php.net/note/delete/125616/in+docs Del: other reasons-- https://master.php.net/note/delete/125616 Reject -- https://master.php.net/note/reject/125616 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215997) next »