[英]Set nodeValue content html in DOMDocument
I have created new element with dom object: 我用dom对象创建了新元素:
$doc = new \DOMDocument();
$link = $doc->createElement('a');
$link->setAttribute('href', '/#');
$link.nodeValue = '<b>Text</b>';
$html = $this->doc->saveHTML();
This variable "$html" contains content: 此变量“ $ html”包含以下内容:
<a href="/#"><b>Text</b></a>
I want to output: 我想输出:
<a href="/#"><b>Text</b></a>
How can I set "nodeValue" correctly? 如何正确设置“ nodeValue”? Is it possible to do it this way?
有可能这样做吗?
Thank you very much. 非常感谢你。
By using a DOMDocumentFragement and it's appendXML() method 通过使用DOMDocumentFragement及其appendXML()方法
<?php
$doc = new \DOMDocument();
$link = $doc->appendChild($doc->createElement('html'))
->appendChild( $doc->createElement('body') )
->appendChild( $doc->createElement('a') );
$link->setAttribute('href', '/#');
$fragment = $doc->createDocumentFragment();
$fragment->appendXML('<b>text</b>');
$link->appendChild($fragment);
echo $doc->saveHTML();
prints 版画
<html><body><a href="/#"><b>text</b></a></body></html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.