[英]Set nodeValue content html in DOMDocument
我用dom对象创建了新元素:
$doc = new \DOMDocument();
$link = $doc->createElement('a');
$link->setAttribute('href', '/#');
$link.nodeValue = '<b>Text</b>';
$html = $this->doc->saveHTML();
此变量“ $ html”包含以下内容:
<a href="/#"><b>Text</b></a>
我想输出:
<a href="/#"><b>Text</b></a>
如何正确设置“ nodeValue”? 有可能这样做吗?
非常感谢你。
通过使用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();
版画
<html><body><a href="/#"><b>text</b></a></body></html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.