【发布时间】:2016-02-08 03:28:28
【问题描述】:
我如何安全匹配所有 <style> 使用 preg_match_all() 在正文中的块?
Google 今天不是我的朋友了。
$haystack = '<body>
<style>
.class {
foo: bar;
}
</style>
<p>Hello World</p>
<style>
/* A comment for <p> */
.class > p {
this: that;
}
</style>
<p>Some HTML</p>
</body>';
preg_match_all('#<style>([^<]+)#is', $haystack, $matches, PREG_SET_ORDER);
var_dump($matches);
preg_match_all('#<style>(.*)</style>#is', $haystack, $matches, PREG_SET_ORDER);
var_dump($matches);
不起作用,因为它与样式注释中的
【问题讨论】:
-
Regex 不是安全处理 HTML 的好工具。
标签: php html regex preg-match-all