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

search for in the

DOMDocument::saveXML> <DOMDocument::saveHTML
[edit] Last updated: Fri, 25 May 2012

view this page in

DOMDocument::saveHTMLFile

(PHP 5)

DOMDocument::saveHTMLFile 内部のドキュメントを HTML 形式でファイルに出力する

説明

int DOMDocument::saveHTMLFile ( string $filename )

DOM 表現から HTML ドキュメントを作成します。この関数は、通常は以下の例のように DOM ドキュメントを新しく作成した後にコールされます。

パラメータ

filename

保存された HTML ドキュメントへのパス。

返り値

書き込んだバイト数、あるいはエラーが発生した場合は FALSE を返します。

例1 HTML ツリーをファイルに保存する

<?php

$doc 
= new DOMDocument('1.0');
// 出力はきれいに整形したいですね。
$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

?>

参考



add a note add a note User Contributed Notes DOMDocument::saveHTMLFile
deep42thouSPAMght42 at y_a_h_o_o dot com 14-Jan-2011 03:46
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 | stats | sitemap | contact | advertising | mirror sites