DOMDocument->saveXML()> <DOMDocument->saveHTML()
Last updated: Mon, 28 Dec 2009

DOMDocument->saveHTMLFile()

(No version information available, might be only in CVS)

DOMDocument->saveHTMLFile() — Dumps the internal document into a file using HTML formatting

Descrição

DOMDocument
int saveHTMLFile ( string $filename )

Creates an HTML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.

Parâmetros

filename

The path to the saved HTML document.

Valor Retornado

Returns the number of bytes written or FALSE if an error occurred.

Exemplos

Example#1 Saving a HTML tree into a file

<?php

$doc 
= new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput true;

$root $doc->createElement('html');
$root $doc->appendChild($root);

$head $doc->createElement('head');
$head $root->appendChild($head);

$title $doc->createElement('title');
$title $head->appendChild($title);

$text $doc->createTextNode('This is the title');
$text $title->appendChild($text);

echo 
'Wrote: ' $doc->saveHTMLFile("/tmp/test.html") . ' bytes'// Wrote: 129 bytes

?>



User Contributed Notes
DOMDocument->saveHTMLFile()
S Leaf
12-Nov-2007 12:52
It does create valid HTML, it doesn't create valid XHTML.
There is a difference.
dev at asp55-ns dot com
13-Sep-2007 08:26
So one thing I've noticed is that DOMDocument->saveHTMLFile() does not create W3C valid HTML. <br> and <img> nodes aren't closed, leading to errors if you want to validate it.

To fix this, I've created a simple function.

<?php
function valid_saveHTMLFile($in,$outputFile) {
       
$in = preg_replace('/<br(.*?)\/?>/','<br$1/>',$in);
       
$in = preg_replace('/<img(.*?)\/?>/','<img$1/>',$in);
        return
file_put_contents($outputFile,$in);
}
?>

to call it

<?php
valid_saveHTMLFile
($dom->saveHTML(),'pathtofile');
?>

with $dom being your DOMDocument, and 'pathtofile' being...here's a surprise...the path to your save file.

The function returns the exact same thing file_put_contents returns (ie: The function returns the amount of bytes that were written to the file, or FALSE on failure.)

DOMDocument->saveXML()> <DOMDocument->saveHTML()
Last updated: Mon, 28 Dec 2009