我想删除一些带有 id 或 class 的 div,其中包含单词comment
或share
(如:<div id="comment">
、、、、)<div class="header-comment">
,我使用的东西<div id="comment-footer">
<div class="social-share">
preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);
不行。如何做一个正确的正则表达式?这是一些测试代码,我想删除comment
部分并保留content
和footer
,
$htmls = <<<EOT
<div id="content">
Main content.
</div>
<div id="comment">
<ul>
<li class="comment">
<div class="header-comment">
Comment:
<span class="date-comment">8/11/2012, 21:25</span>
</div>
<h4>Some Text</h4>
<p class="test-comment">Blah~~ Blah~~ Blah~~</p>
<div class="share">
<div class="vote">
<a class="vota yes" title="Like">2</a>
<a class="vota no" title="Unlike">0</a>
</div>
</div>
</li>
<li class="comment">
<div class="header-comment">
Comment:
<span class="date-comment">8/11/2012, 23:08</span>
</div>
<h4>Other Text</h4>
<p class="test-comment">Blah~~ Blah~~ Blah~~</p>
<div class="share">
<div class="vote">
<a class="vota yes" title="Like">4</a>
<a class="vota no" title="Unlike">0</a>
</div>
</div>
</li>
</ul>
</div>
<div id="footer">
Footer content.
</div>
EOT;
$htmls = preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);
echo $htmls;