繁体   English   中英

在DOMDocument中设置nodeValue内容html

[英]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="/#">&lt;b&gt;Text&lt;/b&gt;</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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM