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

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>

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.