php glob数字范围(文件名中) [英] php glob number range in filename
问题描述
我检查了另外两个答案,但似乎无法匹配文件名中的数字范围
I've checked two other answers but can't seem to match a range of numbers in file name
我需要将1.jpg匹配到不超过99.jpg的任何内容(没有前导零,没有前缀或后缀),只需将数字jpg
I need to match 1.jpg to anything upto 99.jpg (no leading zeros and no prefix or suffix), just plain digits.jpg
glob("$dir/{1..99}.jpg", GLOB_BRACE);
而且我也尝试过:
glob("$dir/{" . implode(",", range(1, 99)) . "}.jpg", GLOB_BRACE);
以下内容仅将1.jpg匹配到9.jpg,而不匹配10,11等
The following matches only 1.jpg to 9.jpg but not 10,11 etc
glob("$dir/[0-9].jpg", GLOB_BRACE);
以下内容一无所有:
glob("$dir/[0-9]+.jpg", GLOB_BRACE);
我已经在 PHP glob范围小的问题中检查了解决方案,为什么?和 php glob模式匹配以获取任意位数的数字
推荐答案
尝试一下:
glob("$dir/{,[1-9]}[0-9].jpg", GLOB_BRACE);
GLOB_BRACE
选项告诉它识别大括号中的替代项,因此{,[1-9]}
匹配空字符串或非零数字.然后,后面跟任何数字.因此,对于空字符串,它匹配0
至9
,对于非零数字,它匹配10
至99
.
The GLOB_BRACE
option tells it to recognize alternatives in braces, so {,[1-9]}
matches either an empty string or a non-zero digit. Then this is followed by any digit. So with the empty string it matches 0
through 9
, and with the non-zero digit it matches 10
through 99
.
这篇关于php glob数字范围(文件名中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!