DOMDocument::saveHTML()函数是PHP中的内置函数,用于从DOM表示形式创建HTML文档。从头开始构建dom文档后使用此功能。
用法:
string DOMDocument::saveHTML( DOMNode $node = NULL )
参数:该函数接受单个参数$node,该参数是可选的,用于输出文档的子集。
返回值:如果成功,此函数返回HTML文档;如果失败,则返回FALSE。
以下示例程序旨在说明PHP中的DOMDocument::saveHTML()函数:
程序:
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0');
// Create a root element
$root = $domDocument->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 saveHTML() function to create
// an HTML document
echo $domDocument->saveHTML();
?>
输出:
DOMDocument::saveHTML() function