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>
.class {
  foo: bar;
<p>Hello World</p>
/* A comment for <p> */
.class > p {
  this: that;
<p>Some HTML</p>

preg_match_all('#<style>([^<]+)#is', $haystack, $matches, PREG_SET_ORDER);

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

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

  • 写回答

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:

    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.

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



  • ¥15 输出区间内所有的完数的个数。 要简单点的程序
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 串口发送数据和接收数据
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码