downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

DOMDocument::saveXML> <DOMDocument::saveHTML
[edit] Last updated: Wed, 22 May 2013

view this page in

DOMDocument::saveHTMLFile

(PHP 5)

DOMDocument::saveHTMLFile Copia el documento interno a un fichero usando el formato HTML

Descripción

int DOMDocument::saveHTMLFile ( string $filename )

Crea un documento HTML desde la representación DOM. Esta función normalmente se llama después de construir un nuevo documento desde cero, como en el ejemplo de abajo.

Parámetros

filename

La ruta al documento HTML guardado.

Valores devueltos

Devuelve el número de bytes escritos o FALSE si ocurrió un error.

Ejemplos

Ejemplo #1 Guardar un árbol HTML en un archivo

<?php

$doc 
= new DOMDocument('1.0');
// queremos una impresión buena
$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('Este es el título');
$text $title->appendChild($text);

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

?>

Ver también



add a note add a note User Contributed Notes DOMDocument::saveHTMLFile - [2 notes]
up
0
naebeth at hotmail dot NOSPAM dot com
7 months ago
Not mentioned in the documentation is the fact that using DOMDocument::saveHTMLFile() will automatically overwrite the contents if an existing file is used - with no notice, warning or error thrown.

Make sure you check the filename before using this function so that you don't accidentally overwrite important files.

Example:
<?php

$file
= fopen('test.html', 'w');
fwrite($file, 'this is some text');
fclose($file);

$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadHTML('<html><head><title>Test</title></head><body></body></html>');
$doc->saveHTMLFile('test.html');

// test.html
/*
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body></body>
</html>
*/

?>

If you're dynamically generating a series of pages using DOMDocument objects, make sure you are also dynamically generating the file or directory names using something that can't easily be confused for an existing file/folder, or check if the desired path already exists before saving so that you don't accidentally delete previous files.
up
0
deep42thouSPAMght42 at y_a_h_o_o dot com
2 years ago
I foolishly assumed that this function was equivalent to
<?php
file_put_contents
($filename, $document->saveHTML());
?>
but there are differences in the generated HTML:
<?php
$doc
= new DOMDocument();
$doc->loadHTML(
   
'<html><head><title>Test</title></head><body></body></html>'
);
$doc->encoding = 'iso-8859-1';

echo
$doc->saveHTML();
#<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#<html>
#<head><title>Test</title></head>
#<body></body>
#</html>

$doc->saveHTMLFile('output.html');
#<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Test</title></head><body></body></html>

?>
Note that saveHTMLFile() adds a UTF-8 meta tag despite the ISO-8859-1 document encoding.

 
show source | credits | sitemap | contact | advertising | mirror sites