php preg_match_all kills page for unknown reason

2019-08-20 09:29发布

问题:

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