php preg_match 匹配错误的主题?

发布于 2024-11-29 20:15:23 字数 1151 浏览 0 评论 0原文

我真的不明白为什么,但是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水溶 2024-12-06 20:15:23

您需要通过在“.”(点)前面添加反斜杠来转义它,ex \.

$filename = "dvdrip.bin"

preg_match_all('/(\.bin|\.d64|\.dcr|\.gb|\.gbc|\.htm|.html|\.nes\|.prg|\.sna|\.swf|\.z80)/i', $filename, $match, PREG_PATTERN_ORDER);
$match = $match[1][0];

echo $match // echo's ".bin" 

You need to escape the "." (dot) by putting a backslash before it, ex \.

$filename = "dvdrip.bin"

preg_match_all('/(\.bin|\.d64|\.dcr|\.gb|\.gbc|\.htm|.html|\.nes\|.prg|\.sna|\.swf|\.z80)/i', $filename, $match, PREG_PATTERN_ORDER);
$match = $match[1][0];

echo $match // echo's ".bin" 
伊面 2024-12-06 20:15:23

您需要转义句号,\. 而不是 .。否则,. 匹配任何字母,因此“arnes”匹配 .nes

You need to escape your periods, \. instead of .. Otherwise the . matches any letter, so "arnes" matches .nes.

忱杏 2024-12-06 20:15:23

你需要逃避“。”因为它会匹配任何字符。因此,请使用 \.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文