preg_replace:错误的正则表达式 == '未知修饰符'? [英] preg_replace: bad regex == 'Unknown Modifier'?

查看:47
本文介绍了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']);

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

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.

澄清:我不想容纳所有有效的电子邮件地址(对我来说是不必要的),我只需要弄清楚我的 preg_replace 正则表达式有什么问题.

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.

推荐答案

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

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.

只需删除 g:

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

这篇关于preg_replace:错误的正则表达式 == '未知修饰符'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆