When importing a node from one DOMDocument Object to another DOMDOcument Object, the nodeValue seems to be lost.
Q1. Anyone knows why? (is this a bug?)
Q2. How can I fix this?
Example code:
$doc1 = new DOMDocument();
$div = $doc1->createElement('div');
$doc2 = new DOMDocument();
$span = $doc2->createElement('span', 'Span text');
$spanCopy = $doc1->importNode($span);
$div->appendChild($spanCopy);
$doc1->appendChild($div);
$otp = $doc1->saveHTML();
var_dump($span->nodeValue);
var_dump($spanCopy->nodeValue);
var_dump($otp);
Outputs:
string 'Span text' (length=9)
string '' (length=0)
string '<div><span></span></div>' (length=25)
Answer:
It seems that in order to perform a deep copy, you need to pass true as second argument to importNode.
$spanCopy = $doc1->importNode($span, true);
Answer:
It seems that in order to perform a deep copy, you need to pass true as second argument to importNode.