Home > Backend Development > PHP Tutorial > 正则表达式 - 这段正则PHP程序为什么崩溃了?

正则表达式 - 这段正则PHP程序为什么崩溃了?

WBOY
Release: 2016-06-06 20:23:49
Original
1001 people have browsed it

<code><?php $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;}
";
$str=ycs($str);
function ycs($str){
    $str = preg_replace_callback("/<style>([^<])+<\/style>/is", function($matched){
      return "abc";
}, $str);
return $str;
}
?>
</style></code>
Copy after login
Copy after login

运行是崩溃了,经过测试,,只要把

<code>/<style>([^<])+<\/style>/is</style></code>
Copy after login
Copy after login

改成

<code>'/<style>[^<]+<\/style>/is', </style></code>
Copy after login
Copy after login

也就是去掉括号,就不崩溃了,为什么会这样,是PHP性能的问题吗???我用的是xampp的运行环境

回复内容:

<code><?php $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;}
";
$str=ycs($str);
function ycs($str){
    $str = preg_replace_callback("/<style>([^<])+<\/style>/is", function($matched){
      return "abc";
}, $str);
return $str;
}
?>
</style></code>
Copy after login
Copy after login

运行是崩溃了,经过测试,,只要把

<code>/<style>([^<])+<\/style>/is</style></code>
Copy after login
Copy after login

改成

<code>'/<style>[^<]+<\/style>/is', </style></code>
Copy after login
Copy after login

也就是去掉括号,就不崩溃了,为什么会这样,是PHP性能的问题吗???我用的是xampp的运行环境

如果你要取 <style></style> 之间的值 那注意应该是这样写 /<style>([^<]+)<\/style>/is</style>
括号要包住加号。你这样的写法,不是正确的正则写法,并不知道会发生什么。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template