2012-11-17 61 views
1

我正在使用php Querypath,我試圖從html字符串中刪除所有html標籤上面和下面的空標籤。例如:使用php querypath刪除html字符串頂部和底部的空標籤

 <div>&nbsp;</div> 
     <div>&nbsp;</div> 
     <div>I want remove empty div tag on top and bottom</div> 
     <div>&nbsp;</div> 
     <div>other content</div> 
     <div>&nbsp;</div> 
     <div>&nbsp;</div> 

用PHP的QueryPath我想改變成:

 <div>I want remove empty div tag on top and bottom</div> 
     <div>&nbsp;</div> 
     <div>other content</div> 

我該怎麼辦?

+1

所以你想保留一個' '在中間。然後你嘗試使用PHP DOM,http://php.net/manual/en/book.dom.php –

回答

0

使用替換字符串將空格替換爲div。

str_replace(find,replace,from); 

所以,你可以在查找使用<div>&nbsp;</div>""從您的代碼替換它。

+0

謝謝。我正在嘗試這一點,但與str_replace也取代

 
是在我想保存它的內容的中間。我只想替換上下不在中間的那些。你有想法嗎? – paganotti

+0

因此,您只想替換'

'部分,但不是像' '那樣的中間部分是對的嗎? – 2012-11-17 15:03:25

+0

不完全。我想刪除整個標籤

 
但只有那些出現在html的頂部和底部。如果你看到上面的例子5,「
 
」存在於html字符串中。我想先刪除,然後第二個「
 
」,然後第四個和第五個。它必須保持第三,因爲它位於「
I want remove empty div tag on top and bottom
」和「
other content
」之間 – paganotti

相關問題