簡體   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