Php 文档格式

Php 文档格式,php,domdocument,Php,Domdocument,我试图阅读某个网页的正文以显示在单独的网页上,但我遇到了一点问题。现在,我使用以下代码 <?php @$doc = new DOMDocument(); @$doc->loadHTMLFile('http://foo.com'); @$tags = $doc->getElementsByTagName('body'); foreach ($tags as $tag) { $index_text .= $tag->nodeValue; print nl2br

我试图阅读某个网页的正文以显示在单独的网页上,但我遇到了一点问题。现在,我使用以下代码

<?php
@$doc = new DOMDocument();
@$doc->loadHTMLFile('http://foo.com');
@$tags = $doc->getElementsByTagName('body');
foreach ($tags as $tag) {
    $index_text .= $tag->nodeValue;
    print nl2br($tag->nodeValue).'<br />';
}
?>


这段代码可以工作,但是它似乎删除了很多格式,这对我来说很重要,比如换行符。如何阻止这种情况发生

DOMDocument的
formatOutput
属性将执行此操作

$doc->formatOutput = true;
这将导致DOM输出更多地用于人类消费,在需要它们的地方使用换行符和缩进,即“漂亮的打印”

此值的默认值为
false
,因此需要时必须将其显式设置为
true