问题描述
我正在尝试验证一堆文本并检查其中是否有任何电子邮件...因此我使用以下代码:
I am trying to validate a bunch of text and check if there are any emails in it... so i use the following code:
if (preg_match_all("/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+/", $str, $matches)){
}
这将适用于Page1中的TEXT_A
this will work for TEXT_A in Page1
但是当我进入Page2并尝试再次检查TEXT_A时它将杀死带有问题加载页面错误的页面...
but when i go in Page2 and try to check again on TEXT_A it will kill the page with "Problem loading page" error...
如果我删除此检查页面将很好加载...我不知道为什么会这样...
if i remove this check the page will load fine... i dont get why this is happening...
edit:im使用CodeIgniter
edit:im using CodeIgniter
推荐答案
正则表达式中的一个问题,该问题可能导致。
I see a problem in your regex that could cause Catastrophic Backtracking.
^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+
^^^^^^^^^^^^^^^^^^^^^^
您想在我标记的区域匹配什么?
What do you want to match in the area I marked?
正则表达式如何知道第二组的哪封信应适用?
How should the regex know from which letter on the second group should apply?
如果您可以匹配的字母序列较长,则正则表达式将需要很多步骤来匹配 ==>您遇到了性能问题,正则表达式只是不及时完成!
If you have a longer sequence of letters that can match, the regex will need a lot of steps to match that ==> you have a performance problem, the regex just don't finish in time!
我想说,您可以删除最后一组,而正则表达式将匹配相同的组,但是速度要快得多。
I would say you can just remove the last group and the regex will match the same, but much faster.
^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+
这篇关于php preg_match_all因未知原因杀死页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!