doudiemei2013 2016-02-08 03:28
浏览 38
已采纳

如何preg_match所有样式标签? [重复]

This question already has an answer here:

How do I safe match all <style> blocks in body using preg_match_all()?

Google is not my friend today.

$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);

Did not work, as it matched the < in the style comment.

</div>
  • 写回答

1条回答 默认 最新

  • dounangshen6553 2016-02-08 03:34
    关注

    Regular expression quantifiers are greedy by default, meaning they match as much as possible. To match as few characters as possible, change the quantifier to lazy (aka non-greedy) by adding a ? after .* for the following:

    preg_match_all('#<style>(.*?)</style>#is', $haystack, $matches, PREG_SET_ORDER);
    

    You can read more about greedy and lazy quantifiers here:

    http://php.net/manual/en/regexp.reference.repetition.php

    It's better to use a HTML parser as your regexp may not match all HTML you encounter. For example, the above regexp will not work for <style type="text/css">. You could change the regexp to something like <style[^><]*> but it's better to use a HTML parser if you can.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?