Maison > Article > développement back-end > 正则除去一些DIV内的内容
正则去除一些DIV内的内容。
我想正则去除一些DIV的内容。div id 或者class 带comment或者share的(comment和share是一个模糊匹配。),比如
$htmls = <<<EOT<br /> <div id="content"><br /> Main content.<br /> </div><br /> <div id="comment"><br /> <ul><br /> <li class="comment"><br /> <div class="header-comment"><br /> Comment:<br /> <span class="date-comment">8/11/2012, 21:25</span><br /> </div><br /> <h4>Some Text</h4><br /> <p class="test-comment">Blah~~ Blah~~ Blah~~</p><br /> <div class="share"><br /> <div class="vote"><br /> <a class="vota yes" title="Like">2</a><br /> <a class="vota no" title="Unlike">0</a><br /> </div><br /> </div><br /> </li><br /> <li class="comment"><br /> <div class="header-comment"><br /> Comment:<br /> <span class="date-comment">8/11/2012, 23:08</span><br /> </div><br /> <h4>Other Text</h4><br /> <p class="test-comment">Blah~~ Blah~~ Blah~~</p><br /> <div class="share"><br /> <div class="vote"><br /> <a class="vota yes" title="Like">4</a><br /> <a class="vota no" title="Unlike">0</a><br /> </div><br /> </div><br /> </li> <br /> </ul><br /> </div><br /> EOT;<br /> <br /> $htmls = preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);<br /> echo $htmls;
</div><br /> <div id="footer"><br /> Footer content.<br /> </div><br /> EOT;