Search code examples
phpxmldomentities

PHP DOM DomText vs DomElement treatment of entities


Why is there a difference in treatment of entities when using DomElement versus DomText?

example code:

$text = 'this&that or this& that';
$document = new \DOMDocument;

$p1 = $document->createElement('p', $text);
// versus
$p2 = $document->createElement('p');
$p2->appendChild($document->createTextNode($text));

var_dump($p1->nodeValue); // thisthat
var_dump($p2->nodeValue); // this&that or this&that

Solution

  • the difference is that the createElement does not accept (accents) and illegal characters in markup and if so, will ignore this kind of character