简体   繁体   English

用PHP编号REGEXP

[英]Number REGEXP with PHP

I try to accept file names from 01.jpg to 12.jpg with this regexp : 我尝试使用此regexp接受从01.jpg到12.jpg的文件名:

preg_match('/^([0-1][0-2]\.jpe?g)$/i', $_FILES['Filedata']['name'])

01.jpg, 10.jpg, 11.jpg, 12.jpg are OK 01.jpg,10.jpg,11.jpg,12.jpg可以

but not 02.jpg to 09.jpg !!! 但不是02.jpg到09.jpg !!!

Thanks for your help ! 谢谢你的帮助 !

尝试这个

 '/^(0[1-9]|1[0-2])\.jpe?g$/i'

You need: 你需要:

^(0[1-9]|1[012])\.jpe?g$

0[1-9] takes care of 01 , 02 and so on till 09 0[1-9]照顾的0102 ,并以此类推,直到09

1[012] which is same as 1[0-2] takes care of 10 , 11 and 12 1[012]这是相同1[0-2]需要的护理101112

Note the use of () carefully. 请注意()的使用。 Since | 由于| has the least precedence in regex operators: 在正则表达式运算符中的优先级最低:

^(0[1-9]|1[0-2]\.jpe?g)$ 

(from the other upvoted answer) is incorrect as it is treated as: (来自其他赞同的答案)不正确,因为它被视为:

^(0[1-9] 

OR 要么

1[0-2]\.jpe?g)$ 

See it 看见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM