在 PHP 中使用 preg_match 时出现“未知修饰符 'g' in...”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3578671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:17:31  来源:igfitidea点击:

"Unknown modifier 'g' in..." when using preg_match in PHP?

phpregexpreg-matchpreg-match-all

提问by Nike

This is the regex I'm trying to use:

这是我尝试使用的正则表达式:

/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim

I found it on this site, and it works great when I try it out there. But as soon as I place it in my code, I get this message:

我在这个网站上找到了它,当我在那里尝试时效果很好。但是一旦我将它放入我的代码中,我就会收到以下消息:

Warning: preg_match() [function.preg-match]: Unknown modifier 'g' in C:\xampp\htdocs\swebook\includes\classes.php on line 22

Can anyone explain what's wrong, and why it's working on that website and not in my code?

谁能解释一下出了什么问题,为什么它在那个网站上而不是在我的代码中工作?

回答by codaddict

There is no modifier gfor preg_match. Instead, you have to use the preg_match_allfunction.

没有修饰gpreg_match。相反,您必须使用该preg_match_all功能。

So instead of:

所以而不是:

preg_match("/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim", ....)

use:

用:

preg_match_all("/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/im", ....)