DOMDocument::saveHTML()函數是PHP中的內置函數,用於從DOM表示形式創建HTML文檔。從頭開始構建dom文檔後使用此函數。
用法:
string DOMDocument::saveHTML( DOMNode $node = NULL )
參數:該函數接受單個參數$node,該參數是可選的,用於輸出文檔的子集。
返回值:如果成功,此函數返回HTML文檔;如果失敗,則返回FALSE。
以下示例程序旨在說明PHP中的DOMDocument::saveHTML()函數:
程序:
<?php
// 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();
?>
輸出:
<html> <head> <title>DOMDocument::saveHTML() function</title> </head> <body> <h1>GeeksforGeeks</h1> <h2>DOMDocument::saveHTML() function</h2> </body> </html>
參考: https://www.php.net/manual/en/domdocument.savehtml.php
相關用法
- PHP DOMDocument createElement()用法及代碼示例
- PHP DOMDocument saveXML()用法及代碼示例
- PHP DOMDocument createComment()用法及代碼示例
- PHP DOMDocument createAttribute()用法及代碼示例
- PHP DOMDocument importNode()用法及代碼示例
- PHP DOMDocument getElementsByTagName()用法及代碼示例
- PHP DOMDocument __construct()用法及代碼示例
- PHP DOMDocument createCDATASection()用法及代碼示例
- PHP DOMDocument loadHTMLFile()用法及代碼示例
- PHP DOMDocument loadHTML()用法及代碼示例
- PHP DOMDocument createAttributeNS()用法及代碼示例
- PHP DOMDocument load()用法及代碼示例
- PHP DOMDocument normalizeDocument()用法及代碼示例
- PHP DOMDocument saveHTMLFile()用法及代碼示例
- PHP DOMDocument save()用法及代碼示例
注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | DOMDocument saveHTML() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。