saveHTMLFile() always saves the file in UTF-8. Even if the DOMDocument->encoding explicitly prescribe different from UTF-8 encoding. All "non-Latin" characters will be converted to HTML-entities. Tested in PHP 5.2.9-2 and PHP 5.2.17. Example:
<?php
$document=new domDocument('1.0', 'WINDOWS-1251');
$document->loadHTML('<html><head><title>Russian language</title></head><body>Русский язык</body></html>');
$document->formatOutput=true;
$document->encoding='WINDOWS-1251';
echo "Записано байт. Recorded bytes: ".$document->saveHTMLFile('html.html');
?>
Method recorded file in UTF-8 encoding. The contents of the file html.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>Russian language</title>
</head>
<body>Ðóññêèé ÿçûê</body>
</html>
PHP.mk документација
DOMDocument::saveHTMLFile
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
domdocument.savehtmlfile.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
domdocument.savehtmlfile.php
DOMDocument::saveHTMLFile
Референца за `domdocument.savehtmlfile.php` со подобрена типографија и навигација.
DOMDocument::saveHTMLFile
класата mysqli_driver
DOMDocument::saveHTMLFile — Го исфрла внатрешниот документ во датотека користејќи HTML формат
= NULL
Создава HTML документ од DOM претставата. Оваа функција обично се повикува по создавање нов DOM документ од нула како во примерот подолу.
Параметри
filename-
Патеката до зачуваниот HTML документ.
Вратени вредности
Враќа број на запишани бајти или false аргумент, или
Примери
Пример #1 Зачувување на HTML дрво во датотека
<?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
?>Види Исто така
- DOMDocument::saveHTML() - Исфрли го внатрешното XML дрво назад во датотека
- DOMDocument::loadHTML() - Вчитај XML од датотека
- DOMDocument::loadHTMLFile() - Вчитај HTML од стринг
Белешки од корисници 3 белешки
RiKdnUA на mail dot ru ¶
12 години пред
naebeth at hotmail dot NOSPAM dot com ¶
пред 13 години
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.
deep42thouSPAMght42 at y_a_h_o_o dot com ¶
пред 15 години
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.