preg_replace:错误的正则表达式==“未知修饰符”?

发布于 2024-12-08 07:31:19 字数 486 浏览 1 评论 0原文

我正在编造虚假的电子邮件地址,我只是想确保它们采用有效的电子邮件格式,因此我尝试删除不在以下设置中的任何字符:

$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/g', '', $jusr['email']);

我在 Windows 计算机上没有遇到任何问题,但在 Linux 开发服务器上,每次运行此代码时我都会收到此错误:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in /var/www/vhosts/....

我认为这是正则表达式字符串,但我无法确定它。帮助不大?谢谢。

澄清:我并不是想容纳所有有效的电子邮件地址(这对我的目的来说是不必要的),我只是需要找出我的 preg_replace 正则表达式出了什么问题。

I'm making up fake email addresses and I just want to make sure they are in a valid email format so I'm trying to remove any character that is not in the set below:

$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/g', '', $jusr['email']);

I haven't had any trouble on my windows machine, but on the linux dev server I get this error each time this code runs:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in /var/www/vhosts/....

I think it's the regex string, but I can't pin it down. Little help? Thanks.

Clarification: I'm not trying to accommodate all valid email addresses (unnecessary for my purpose), I just need to figure out what's wrong with my preg_replace regex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

你与昨日 2024-12-15 07:31:19

g 不是 PCRE(PHP 使用的正则表达式实现)中的有效修饰符,因为根本不需要它; preg_replace() 默认情况下将执行全局替换。您会在真正的 Perl 正则表达式和 JavaScript 正则表达式中找到该修饰符,但在 PCRE 中找不到。

只需删除 g

$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/', '', $jusr['email']);

g is not a valid modifier in PCRE (the regex implementation PHP uses) because it's simply not needed; preg_replace() will perform global replacements by default. You'll find the modifier in true Perl regex as well as JavaScript regex, but not in PCRE.

Just drop the g:

$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/', '', $jusr['email']);
七婞 2024-12-15 07:31:19

您的 PCRE 修饰符无效。以下是有效 PCRE 修饰符的列表:

http://us. php.net/manual/en/reference.pcre.pattern.modifiers.php

g(全局)修饰符默认启用,因此您不需要不需要它。

You have an invalid PCRE modifier. Here is the list of valid PCRE modifiers:

http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php

The g (global) modifier is on by default, so you don't need it.

煮酒 2024-12-15 07:31:19

问题是 g 不是 有效的 PCRE 修饰符。尝试查看 preg_match_all

The problem is that g is not a valid PCRE modifier. Try looking at preg_match_all.

嘿哥们儿 2024-12-15 07:31:19

除了 /g 之外,正则表达式的内部部分似乎也无效:

[^a-zA-Z0-9.-_@]

首先,“^”(输入开始元字符)在 [...] 内部没有任何意义(除非您允许包含“^”的电子邮件地址)。其次,破折号应该被转义或放在组的末尾,否则它将被视为范围运算符。最重要的是,您的表达式不允许使用各种完全有效的电子邮件地址。查看一些示例

In addition to /g, the inner part of your regexp doesn't seem to be valid either:

[^a-zA-Z0-9.-_@]

First, the "^" (which is start-of-input metachar) makes no sense inside [...] (unless you allow email adresses that contain "^"). Second, the dash should be escaped or put to the end of the group, otherwise it will be treated as range operator. And most important, your expression disallows a wide range of perfectly valid email addresses. Check some examples.

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