php preg_match_all kills page for unknown reason

给你一囗甜甜゛ 提交于 2020-01-06 05:49:11

问题


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)){

}

this will work for TEXT_A in Page1

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 using CodeIgniter


回答1:


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])+


来源:https://stackoverflow.com/questions/9447837/php-preg-match-all-kills-page-for-unknown-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!