PHP服务器端验证正则表达式匹配

我有以下验证脚本部分:</ p>

   $ invalidEmailError。=“&lt; br /&gt;&amp; raquo;&amp; nbsp;您没有输入有效的电子邮件地址”;

$ match =“/\ b[A-Z0-9._%+-]+@[A-Z0-9.-]+\。[AZ] {2,400} b /”; n </ code> </ pre>

这是表达式,这里是验证:</ p>

  if(!(preg_match($ match,$ email)  )){
$ errors。= $ invalidEmailError; //检查电子邮件的有效性
}
</ code> </ pre>

我认为这是足够的信息,请告诉我是否需要更多信息。</ p>

基本上,发生的是“你没有输入有效的电子邮件地址”的消息,无论什么</ em>都会回显。 是否输入了正确的电子邮件地址或错误的电子邮件地址。</ p>

有没有人对此有任何想法或线索?</ p>

编辑:我 我在localhost上运行它(使用Apache),这可能是为什么preg_match无法正常工作的原因吗?</ p>

谢谢!
Amit </ p>
</ div >

展开原文

原文

I have the following part of a validation script:

$invalidEmailError .= "<br/>&raquo;&nbsp;You did not enter a valid E-mail address";    
$match = "/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/";

That's the expression, here is the validation:

if ( !(preg_match($match,$email)) ) {
    $errors .= $invalidEmailError; // checks validity of email
}

I think that's enough info, let me know if more is needed.

Basically, what happens is the message "You did not enter a valid E-mail address" gets echoed no matter what. Whether a correct email address or an incorrect email address is entered.

Does anyone have any idea or a clue as to why?

EDIT: I'm running this on localhost (using Apache), could that be the reason as to why the preg_match ain't working?

Thanks! Amit

douxie2007
douxie2007 此外,你能回复$email,看看它给你带来了什么?
大约 10 年之前 回复
dsolwotv00116
dsolwotv00116 请参阅fightingforalostcause.net/misc/2006/compare-email-regex.php
大约 10 年之前 回复

3个回答



你的正则表达式只包括 [A-Z] </ code>,而不是 [a-z] </ code>。 尝试</ p>

  $ match =“/\ b [A-Z0-9._%+  - ] +@ [A-Z0-9.-] + \。[AZ  ] {2,4} \ b / i“; 
</ code> </ pre>

使正则表达式不区分大小写。</ p>

你可以 在 http://regexpal.com 上进行实时测试。</ p>

但是,我 建议您尝试 strager 提到的页面上的一个表达式: http://fightingforalostcause.net/misc/2006/compare-email-regex.php 。 随着时间的推移,它们已经完善,可能会表现得更好。 但Gmail用户会对您的用户满意,因为他们可以使用加上别名,被许多验证器错误地拒绝。</ p>
</ div>

展开原文

原文

Your regex only includes [A-Z], not [a-z]. Try

$match = "/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i";

to make the regex case-insensitive.

You can test this live on http://regexpal.com.

However, I'd advise you to try one of the expressions on the page mentioned by strager: http://fightingforalostcause.net/misc/2006/compare-email-regex.php. They have been perfected over time and will probably behave better. But Gmail users will be satisfied with yours, since they'll be able to use plus aliases which are rejected incorrectly by many validators.

doujiurong7210
doujiurong7210 完美,这就行了。 一旦SO让我:)将此标记为正确答案:)
大约 10 年之前 回复



您可能从 regular-expressions.info 。 在该页面上,作者声明(强调添加):</ p>


如果您想使用上面的正则表达式,您需要了解两件事。 首先,长正则表达式很难很好地格式化段落。 所以我没有在三个角色类中的任何一个中包含a-z。 此正则表达式旨在与您的正则表达式引擎的“不区分大小写”选项一起使用。</ strong>(您会惊讶地发现了多少“bug”报告。)其次,上面的正则表达式是 用单词边界分隔,这使它适合从文件或更大的文本块中提取电子邮件地址。 如果要检查用户是否输入了有效的电子邮件地址,请将字边界替换为字符串开头和字符串结尾锚点,如下所示:^ [A-Z0-9 ._%+ - ] + @ [A-Z0-9 .-] +。[AZ] {2,4} $。</ p>
</ blockquote>

要解决此问题,请添加 i </ code>正则表达式后的PCRE标志。</ p>
</ div>

展开原文

原文

You likely got the regular expression you're using from regular-expressions.info. On that page, the author states (emphasis added):

If you want to use the regular expression above, there's two things you need to understand. First, long regexes make it difficult to nicely format paragraphs. So I didn't include a-z in any of the three character classes. This regex is intended to be used with your regex engine's "case insensitive" option turned on. (You'd be surprised how many "bug" reports I get about that.) Second, the above regex is delimited with word boundaries, which makes it suitable for extracting email addresses from files or larger blocks of text. If you want to check whether the user typed in a valid email address, replace the word boundaries with start-of-string and end-of-string anchors, like this: ^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$.

To solve this problem, add the i PCRE flag after your regular expression.

dougaopu7938
dougaopu7938 好吧,你去吧,哈哈。 显然我读书很糟糕!! 谢谢你为我清理。
大约 10 年之前 回复



您总是可以尝试使用更简单的工具调试您的正则表达式(我非常喜欢使用Notepad ++来实现此目的)并执行迭代测试 - 即。 使表达式更复杂/更简单,并查看是否修复/破坏了事物。</ p>
</ div>

展开原文

原文

You can always try debugging your regex using a simpler tool (I'm quite fond of using Notepad++ for this purpose) and performing iterative tests - ie. making the expression more/less complicated and seeing if that fixes/breaks things.

doushi1510
doushi1510 谢谢:)我使用Notepad ++作为我的编辑器,它真棒。
大约 10 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问