【问题标题】:preg_match_all grab inside html commentpreg_match_all 抓取内部 html 注释
【发布时间】:2011-11-07 04:31:31
【问题描述】:

在标题中看到,这里是 HTML 代码示例:

<body>
<!--CODE_START-->
<p>I <strong>Want</strong> this</p>
<p>And this one too</p>
<!--CODE_STOP-->

<p>This sould be go to trash</p>

<!--CODE_START-->
<p>This one should be included too</p>
<!--CODE_STOP-->

问题是,我想要&lt;!--CODE_START--&gt;&lt;!--CODE_STOP--&gt; 中的所有内容,所以结果应该是:

<p>I <strong>Want</strong> this</p>
<p>And this one too</p>

&lt;p&gt;This one should be included too&lt;/p&gt;

我尝试将这个/&lt;!--CODE_START--&gt;([^&lt;]*)&lt;!--CODE_STOP--&gt;//&lt;!--CODE_START--&gt;(.*)&lt;!--CODE_STOP--&gt;/suimuim 等模式修饰符组合使用,它不起作用,只返回一个空数组。而且,我试图抓取它的完整 HTML 页面。

提前致谢。

【问题讨论】:

    标签: php pcre preg-match-all


    【解决方案1】:

    [^&lt;] 表示所有不是 p> 处失败。抓住一切并使用非贪婪选项:

    preg_match_all('/<!--CODE_START-->(.*)<!--CODE_STOP-->/sU', $foo);
    

    【讨论】:

    • 对不起,我忘了放这个,我以前试过这个,但它还是一样,有一个空数组结果。
    猜你喜欢
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2011-02-04
    • 1970-01-01
    • 2017-03-29
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多