使用WAMP,我正在尝试使用以下代码使PHP与Windows相同:
<?php
$folder = opendir("folderx");
$fileNameList = array();
while(false !== ($fileName = readdir($folder))){
array_push($fileNameList, $fileName);
}
echo "<pre>";
print_r($fileNameList);
echo "</pre>";
?>
然而,我得到了奇怪的结果。这就是PHP的排序方式:
Array
(
[0] => .
[1] => ..
[2] => New Text Document - Copy (2) - Copy - Copy.txt
[3] => New Text Document - Copy (2) - Copy.txt
[4] => New Text Document - Copy (2).txt
[5] => New Text Document - Copy (3) - Copy.txt
[6] => New Text Document - Copy (3).txt
[7] => New Text Document - Copy (4) - Copy.txt
[8] => New Text Document - Copy (4).txt
[9] => New Text Document - Copy - Copy (2) - Copy.txt
[10] => New Text Document - Copy - Copy (2).txt
[11] => New Text Document - Copy - Copy (3).txt
[12] => New Text Document - Copy - Copy - Copy (2).txt
[13] => New Text Document - Copy - Copy - Copy - Copy.txt
[14] => New Text Document - Copy - Copy - Copy.txt
[15] => New Text Document - Copy - Copy.txt
[16] => New Text Document - Copy.txt
[17] => New Text Document.txt
)
这就是Windows的排序方式:
答案 0 :(得分:2)
如果使用natcasesort($ fileNameList)对数组进行排序,如下所示:
<?php
$folder = opendir("folderx");
$fileNameList = array();
while(false !== ($fileName = readdir($folder))){
array_push($fileNameList, $fileName);
}
function strip_non_cmp_characters($word) {
return str_replace(array("(",")"," ","."),array("","","",""), $word);
}
function wincmp($a,$b) {
return strnatcasecmp(strip_non_cmp_characters($a),
strip_non_cmp_characters($b));
}
usort($fileNameList,"wincmp");
echo "<pre>";
print_r($fileNameList);
echo "</pre>";
答案 1 :(得分:0)
您可以使用scandir按字母顺序排序文件。 Opendir会将它们存储在文件系统中,而不是它们在Windows中显示的内容。