PHP | DOMDocument saveHTMLFile()函数
DOMDocument::saveHTMLFile()函数是PHP中的一个内置函数,用于从 DOM 表示创建 HTML 文档。该函数在创建 dom 文档后使用。
句法:
int DOMDocument::saveHTMLFile( string $filename )
参数:此函数接受单个参数$filename ,其中包含保存 HTML 文档的路径。
返回值:此函数在成功时返回字节数,在失败时返回 FALSE。
下面的程序说明了PHP中的 DOMDocument::saveHTMLFile()函数:
程序:
createElement('html');
// Append the element to the document as root element
$root = $domDocument->appendChild($root);
// Create a head element
$head = $domDocument->createElement('head');
// Append the element to the document
// as child element
$head = $root->appendChild($head);
// Create a title element
$title = $domDocument->createElement('title');
// Append the element to the document
// as child element
$title = $head->appendChild($title);
// Create a text node
$text = $domDocument->createTextNode(
'DOMDocument::saveHTML() function');
// Add the text node the the title element
$text = $title->appendChild($text);
// Create a body element
$body = $domDocument->createElement('body');
// Append the element to the document
// as child element
$body = $root->appendChild($body);
// Create a heading element
$h1 = $domDocument->createElement('h1');
// Append the element to the document
$h1 = $body->appendChild($h1);
// Create a text node
$text = $domDocument->createTextNode('GeeksforGeeks');
// Add the text node to the heading element
$text = $h1->appendChild($text);
// Create a heading element
$h2 = $domDocument->createElement('h2');
// Append the element to the document
$h2 = $body->appendChild($h2);
// Create a text node
$text = $domDocument->createTextNode(
'DOMDocument::saveHTML() function');
// Add the text node to the heading element
$text = $h2->appendChild($text);
// Use saveHTMLFile() function to save
// an HTML document
$domDocument->saveHTMLFile('gfg.html');
echo "HTML file saved successfully";
?>
输出:
HTML file saved successfully
保存的 HTML 文件 gfg.html 的内容:
DOMDocument::saveHTML() function
GeeksforGeeks
DOMDocument::saveHTML() function
参考: https://www. PHP.net/manual/en/domdocument.savehtml 文件。 PHP