我想正则去除一些DIV的内容。div id 或者class 带comment或者share的(comment和share是一个模糊匹配。),比如<div id="comment">, <div class="header-comment">, <div id="comment-footer">等等。下面这段测试代码,保留Main content.去除后面的评论部分。$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>
EOT;$htmls = preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);
echo $htmls;
<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>
EOT;$htmls = preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);
echo $htmls;
解决方案 »
- php读取textarea的问题,在线等
- c++写的程序可以在租的空间了运行吗?
- php Failed opening required 'xxx.php'(include_path='.;C:\php5\pear'是什么情况
- win2003server下配置php+mysql+Apache的问题!!
- select 超链接
- 基于微软 .Net 框架的 PHP 编译器 - Phalanger
- php读取文件失败??
- php如何导入word,然后在word打印,急。。。在线等
- 高分相送!!!php+odbc的查询问题!
- php中怎样访问一个别的网站上的php(网页)?
- PHP 在线等待求一个方法.
- 如何不输出表单 执行post操作
<div id="footer">
Footer content.
</div>
EOT;
我是说如果后面再加一个#footer,这个footer也被去掉了。
然后遍历所有的div,然后
1、获取div的id,检测是否包含comment或者share
1、获取div的class,检测是否包含comment或者share
有则删除这个div,最后输出处理后的html即可