2014-01-16 68 views
-2

我正在通過php腳本清除一些html文件,並且我想刪除所有不在<tag></tag>之間的所有\n thingsies。如何刪除所有換行符在html標籤之外

<p>some text</p> 


      <- here are the bunch of \n I want to remove 


<p>some other random 
text with \n at fixed width 
and that's great</p> 

任何想法? 非常感謝。

+1

您的意思是在''標籤內,但在其任何子標籤之外。 – techfoobar

+0

以外的任何在html代碼中。 不好 Lego

+0

爲此使用DOM解析器,並刪除所有隻是空白的子標記。你有沒有看過DOMDocument的文檔? –

回答

1

這樣的事情就足夠了嗎?

<?php 
$html=<<<SOMECONT 
<p>some text</p> 





<p>some other random 
text with \n at fixed width 
and thats great</p> 
SOMECONT; 

$narr=array_filter(explode(PHP_EOL,$html),'strlen'); 
echo implode('',$narr); 

OUTPUT:

<p>some text</p><p>some other randomtext with 
at fixed widthand thats great</p> 

編輯:另類

可能會更 「髒」,但工程。最後,刪除html標籤之間的所有\ n有時可以像從原始文件的分解字符串中刪除空行一樣簡單。

$split = explode(PHP_EOL,$data); 
    $data= ""; 
    for($i = 0; $i < count($split); $i++){ 
    $line = $split[$i]; 
    else if(strlen($line) > 0) $data .= $split[$i]."\n"; // filter 
    } 
+0

不會刪除html標籤之間的換行符嗎? – Lego

+2

strlen在這裏評估如果長度> 0來刪除每個「空行」權利?它應該工作,但不知道它不是。但是我創建了一個骯髒的方法來在PHP_EOL上使用爆炸來做同樣的事情,然後使用strlen()進行過濾。感謝 – Lego

+0

@樂高,很高興你的工作。你可以編輯這個答案,就像你做的一樣,所以其他人可以從中受益! –

相關問題