This time I will bring you a detailed explanation of the efficient greedy, non-greedy and backtracking of using regularity in PHP (with code), and the efficiency greedy, non-greedy and backtracking of using regularity in PHPNotes What are they? Here are actual cases. Let’s take a look.
Let’s first understand what greed is in regular expressions, and what is non-greedy? Or what is matching priority quantifier and what is ignoring priority quantifier?
Okay, I don’t know what the concept is, let’s give an example.
A student wanted to filter the content between them. This is how he wrote the regular rules and procedures.
$str = preg_replace('%<script>.+?</script>%i','',$str);//非贪婪
It seems that there is no problem, but in fact it is not. If
$str = '<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>';
, then after the above program processing, the result is
$str = '<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>'; $str = preg_replace('%<script>.+?</script>%i','',$str);//非贪婪 print_r($str); //$str 输出为 <script>alert(document.cookie)</script>
still cannot achieve the desired effect. The above is non-greed, and some are called laziness. The non-greedy sign is the quantity meta character followed by ?, such as +?, *?, ?? (more special, I will write about it in future blogs), etc. That means non-greedy. If you don’t write ?, it means greedy. For example,
$str = '<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>'; $str = preg_replace('%<script>.+</script>%i','',$str);//非贪婪 print_r($str); //$str 输出为Latest Articles by Author
2018-06-11 11:57:34 2018-06-15 15:55:18 2018-06-15 15:49:00 2018-06-15 15:46:15 2018-06-15 15:42:38 2018-06-15 15:40:34 2018-06-15 15:39:32 2018-06-15 15:37:14 2018-06-15 15:34:21 2018-06-15 15:22:51Latest IssuesGroup MySQL results by ID for looping over I have a table with flight data in mysql. I'm writing a php code that will group and displ...From 2024-04-06 17:27:5601406Related TopicsMore>