为什么这个 php 正则表达式程序崩溃
Why this php regular expression program crash
here is the code
<?php
//here is the string
$str ="<style>
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
</style>";
$str=ycs($str);
function ycs($str){
$str = preg_replace_callback("/<style>([^<])+<\/style>/is", function($matched){
return "abc";
}, $str);
return $str;
}
echo $str;
?>
like the code i write,the php program crash! But when i change the
code "/([^<])+</style>/is" to "/[^<]+</style>/is"
,it can run again,Why?I use xampp.
你的正则表达式的问题是你一次捕获了除 <
之外的所有内容,但是你通过使用导致丑陋的正则表达式操作的组重复多次此操作(这不应该崩溃但是你的表现会很糟糕。
所以,你可以改变这个:
<style>([^<])+<\/style>
至
<style>([^<]+)<\/style>
以上更改将通过重复模式然后捕获内容来改进正则表达式,我猜你想这样做。
here is the code
<?php
//here is the string
$str ="<style>
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
.a{color: red;}
</style>";
$str=ycs($str);
function ycs($str){
$str = preg_replace_callback("/<style>([^<])+<\/style>/is", function($matched){
return "abc";
}, $str);
return $str;
}
echo $str;
?>
like the code i write,the php program crash! But when i change the code "/([^<])+</style>/is" to "/[^<]+</style>/is" ,it can run again,Why?I use xampp.
你的正则表达式的问题是你一次捕获了除 <
之外的所有内容,但是你通过使用导致丑陋的正则表达式操作的组重复多次此操作(这不应该崩溃但是你的表现会很糟糕。
所以,你可以改变这个:
<style>([^<])+<\/style>
至
<style>([^<]+)<\/style>
以上更改将通过重复模式然后捕获内容来改进正则表达式,我猜你想这样做。