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.
DOMDocument::saveHTMLFile
(PHP 5)
DOMDocument::saveHTMLFile — Dahili belgeyi bir HTML dizge olarak dosyaya yazar
Açıklama
int DOMDocument::saveHTMLFile
( string
$dosya
)HTML belgeyi DOM gösteriminden oluşturur. Bu yöntem genellikle, aşağıdaki örnekte olduğu gibi sıfırdan bir belge oluşturulduktan sonra çağrılır.
Değiştirgeler
-
dosya -
HTML belgenin kaydedileceği dosyanın yolu.
Dönen Değerler
Bir hata oluşmuşsa FALSE yoksa yazılan bayt sayısı döner.
Örnekler
Örnek 1 - Bir HTML ağacının bir dosyaya kaydedilmesi
<?php
$doc = new DOMDocument('1.0');
// Çıktı göze hoş görünsün
$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 $doc->saveHTMLFile("/tmp/test.html") . ' bayt yazıldı';
// Çıktısı: 129 bayt yazıldı
?>
Ayrıca Bakınız
- DOMDocument::saveHTML() - Dahili belgeyi bir HTML dizgesi olarak çıktılar
- DOMDocument::loadHTML() - HTML belgeyi bir dizgeden yükler
- DOMDocument::loadHTMLFile() - HTML belgeyi bir dosyadan yükler
deep42thouSPAMght42 at y_a_h_o_o dot com
14-Jan-2011 03:46
