2013-09-30 75 views
0

我分析網頁內容,並顯示在下面的方式在div:顯示網頁內容與換行符

$doc = new DOMDocument(); 
libxml_use_internal_errors(true); 
$doc->loadHTMLFile($url); 
libxml_clear_errors(); 
$xpath = new DOMXPath($doc); 
foreach($xpath->query("//script") as $script) { 
    $script->parentNode->removeChild($script); 
} 

$textContent = $doc->textContent; //inherited from DOMNode 
$text=escapeshellarg($textContent); 

$test = preg_replace("/[^a-zA-Z]+/", " ", html_entity_decode($text)); 

但是,這也將刪除換行符(段)到$測試。 如何在上面包含換行符以便每個段落都可見?

回答

1

你可以簡單地包含換行符,而不是替換它們。

$test = preg_replace("/[^a-zA-Z\n]+/", " ", html_entity_decode($text)); 
           ^^ 
1

PHP有一個很好的功能。試試這個:

echo nl2br($test); 
+0

謝謝,但它跳過換行符。這裏是我的代碼審查請http://codetidy.com/6796/ – user123