scandir-排序数字文件名 [英] scandir - sort numeric filenames
问题描述
做了一些搜索,但似乎找不到我要找的确切答案.
Done some searching, but can't seem to find the exact answer I'm looking for.
我想使用'scandir($ dir)'提取带有编号文件名的文件,但要使它们正确排序.例如,文件名是:
I'd like to pull in files with numbered filenames using 'scandir($dir)', but have them sort properly. For example, file names are:
1-something.ext
2-something-else.ext
3-a-third-name.ext
.
.
.
10-another-thing.ext
11-more-names.ext
我遇到的问题是10-a-fourththing.ext将在2-something-else.ext之前显示.我想找到一种解决此问题的更好方法,而不是在所有文件名前都添加前导"0".
The problem I'm having is that 10-a-fourth-thing.ext will show before 2-something-else.ext. I'd like to find a better way of solving this issue than introducing leading '0' in front of all file names.
有什么想法吗?谢谢.
推荐答案
natsort
完全满足您的需求.
natsort
does exactly what you need.
sort
也适用于以数字开头的文件名,但是如果前面也没有数字的名称会损坏(所有非数字前缀的名称将在数字前缀之前进行排序)名称及其相对的顺序将是随机的,而不是字母顺序.
sort
with SORT_NUMERIC
will also work for filenames that start with numbers, but it will break if there are also names that have no numbers in front (all non-number-prefixed names will be sorted before number-prefixed names, and their order relative to one another will be random instead of alphabetic).
这篇关于scandir-排序数字文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!