Example Analysis of regular expression efficiency greed, non-greed and backtracking in PHP
This article will explain in detail the example analysis of regular expression efficiency greed, non-greed and backtracking in PHP. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
If a student wants to filter the content between them, that's how he writes the rules and procedures.
$str = preg_replace ('%. +?% iPrecious and not greedy).
It seems that there is no problem, but in fact it is not. If
$str = 'alert ([xss_clean])'
Then after the above program processing, the result is
$str = 'alert ([xss_clean])'; $str = preg_replace ('%. +?); / / non-greedy print_r ($str); / / $str output to alert ([xss_clean])
Still can't get the effect he wants. The above is not greedy, but also called inertia. Its non-greedy logo is followed by a quantifier metacharacter, such as +, *,? (quite special, I will write about it in the future BLOG), etc. That is, the logo is not greedy, if not written? Is greed. such as
$str = 'alert ([xss_clean])'; $str = preg_replace ('%. +% iGray print_r ($str); / / $str output is