savehtml php,PHP DOMDocument saveHTMLFile()用法及代码示例

DOMDocument::saveHTMLFile()函数是PHP中的内置函数,用于从DOM表示形式创建HTML文档。创建dom文档后使用此功能。

用法:

int DOMDocument::saveHTMLFile( string $filename )

参数:该函数接受单个参数$filename,该参数保存保存HTML文档的路径。

返回值:此函数返回成功时的字节数或失败时的FALSE。

以下示例程序旨在说明PHP中的DOMDocument::saveHTMLFile()函数:

程序:

// 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 saveHTMLFile() function to save

// an HTML document

$domDocument->saveHTMLFile('gfg.html');

echo "HTML file saved successfully";

?>

输出:

HTML file saved successfully

保存的HTML文件gfg.html的内容:

content="text/html; charset=UTF-8">

DOMDocument::saveHTML() function

GeeksforGeeks

DOMDocument::saveHTML() function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值