php node html,How to insert HTML to PHP DOMNode?

淳于泓
2023-12-01

Here is simple example by using DOMDocumentFragment:

$doc = new DOMDocument();

$doc->loadXML("");

$f = $doc->createDocumentFragment();

$f->appendXML("texttext2");

$doc->documentElement->appendChild($f);

echo $doc->saveXML();

Here is helper function for replacing DOMNode:

/**

* Helper function for replacing $node (DOMNode)

* with an XML code (string)

*

* @var DOMNode $node

* @var string $xml

*/

public function replaceNodeXML(&$node, $xml) {

$f = $this->dom->createDocumentFragment();

$f->appendXML($xml);

$node->parentNode->replaceChild($f,$node);

}

Source: Some old "PHP5 Dom Based Template" article.

And here is another suggestion posted by Pian0_M4n to use value attribute as workaround:

$dom = new DomDocument;

// main object

$object = $dom->createElement('div');

// html attribute

$attr = $dom->createAttribute('value');

// ugly html string

$attr->value = "

  this is a really html string ©
with all the © that XML hates!";

$object->appendChild($attr);

// jquery fix (or javascript as well)

$('div').html($(this).attr('value')); // and it works!

$('div').removeAttr('value'); // to clean-up

No ideal, but at least it works.

 类似资料:

相关阅读

相关文章

相关问答