Formatting a domdocument

I am trying to read in the body of a specific web page to display on a separate web page, but I have a little problem with it. Right now, I'm using the following code

<?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 />';
}
?>

This code works, however, it seems to remove a lot of formatting, which is important to me, for example, line breaks. How to stop this from happening

+3
source share
1 answer

The formatOutputDOMDocument attribute will do this.

$doc->formatOutput = true;

This will lead to the fact that the output of the DOM will be output more for human consumption, with line breaks where you need them, and indentation, that is, "pretty print".

false, true, .

+7

Source: https://habr.com/ru/post/1714864/


All Articles