Kea*_*her 5 php preg-replace segmentation-fault
当我执行以下代码时; 我每次都会遇到一个段错误!这是一个已知的错误?如何使此代码有效?
<?php
$doc = file_get_contents("http://prairieprogressive.com/");
$replace = array(
"/<script([\s\S])*?<\/ ?script>/",
"/<style([\s\S])*?<\/ ?style>/",
"/<!--([\s\S])*?-->/",
"/\r\n/"
);
$doc = preg_replace($replace,"",$doc);
echo $doc;
?>
Run Code Online (Sandbox Code Playgroud)
错误(显然)看起来像:
[root@localhost 2.0]# php test.php
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
您有不必要的捕获组,这会给 PCRE 的回溯带来压力。尝试这个:
$replace = array(
"/<script.*?><\/\s?script>/s",
"/<style.*?><\/\s?style>/s",
"/<!--.*?-->/s",
"/\r\n/s"
);
Run Code Online (Sandbox Code Playgroud)
另一件事是, (空白)与(非空白)\s组合可以匹配任何内容。\S所以只需使用该.模式即可。