2013-02-08 64 views
0

HTML的div和內容:PHP - 刪除裏面

<!--a lot of HTML before--> 
<div class="quoteheader"> 
    <div class="topslice_quote"><a href="htsomelink">Some text</a></div> 
</div> 
<blockquote class="bbc_standard_quote">Some text<br /> 
</blockquote> 
<div class="quotefooter"> 
    <div class="botslice_quote"></div> 
</div> 
<br /> 
<!--a lot of HTML after--> 

我需要:後div.quoteheader和第一<br/>之間移除一切,所以結果應該是這樣的:

<!--a lot of HTML before--> 
<!--a lot of HTML after--> 

我試過了:

$message = preg_replace('/<div\sclass=\"quoteheader\">[^<]+<\/div>/i', '', $string) 
+0

這將是'之間的一切div.quoteheader'和* second *'
' – 2013-02-08 17:50:56

回答

0

你會比使用正則表達式更好地使用XML/HTML/DOM解析器。 SimpleXML非常簡單。

您只需加載HTML w/SimpleXML或其他HTML/XML解析器,然後使用xpath查找您正在查找的節點和/或註釋,然後將其刪除。

+0

substr&strpos和strip_tags就足夠了 – 2013-02-08 17:46:28

0

替代......如果你能劃帶註釋的代碼,就像這樣:

<!--code--> 
<div> .. </div> 
<!--/code--> 

您可以在之間移除一切:

$newstr = preg_replace('/<!--code-->.*?<!--\/code-->/is', '', $htmlstring); 
0
preg_replace('/(\<div\ class="quoteheader"\>)(.+)(<br \/>)/si', '', $string)