php preg_match 匹配错误的主题?
我真的不明白为什么,但是 PHP preg_match 在某些主题中匹配了错误的主题。
例如,在这 4 行中,我希望得到“匹配 2”作为输出,但我得到的都是匹配的(匹配 1 到 4)
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh.php'))
echo 'match 1';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh.swf'))
echo 'match 2';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh1.gif'))
echo 'match 3';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh2.gif'))
echo 'match 4';
在这种情况下,我得到“匹配 2”作为输出,正如预期的
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang.php'))
echo 'match 1';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang.swf'))
echo 'match 2';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang1.gif'))
echo 'match 3';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang2.gif'))
echo 'match 4';
那样做错了吗?
非常感谢指教!
I really don't understand why, but PHP preg_match is matching wrong subjects in certain ones.
For instance, in this 4 lines, i expect to get "match 2" as output, but i get all as matched instead (match 1 through 4)
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh.php'))
echo 'match 1';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh.swf'))
echo 'match 2';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh1.gif'))
echo 'match 3';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'arnesdemanoTh2.gif'))
echo 'match 4';
In this case, i get "match 2" as output, just as expected
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang.php'))
echo 'match 1';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang.swf'))
echo 'match 2';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang1.gif'))
echo 'match 3';
if (preg_match("/.bin|.d64|.dcr|.gb|.gbc|.htm|.html|.nes|.prg|.sna|.swf|.z80/i", 'bang2.gif'))
echo 'match 4';
What i'm doing wrong?
Thanks a lot in advise!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要通过在“.”(点)前面添加反斜杠来转义它,ex \.
You need to escape the "." (dot) by putting a backslash before it, ex \.
您需要转义句号,
\.
而不是.
。否则,.
匹配任何字母,因此“arnes”匹配.nes
。You need to escape your periods,
\.
instead of.
. Otherwise the.
matches any letter, so "arnes" matches.nes
.你需要逃避“。”因为它会匹配任何字符。因此,请使用 \.bin 而不是“.bin”。在第一组行中,文件名与 .nes 匹配。
you need to escape the "." since it will match any character. so instead of ".bin" use \.bin. in the first set of lines, the name of the file matches with .nes.