I have a problem. PHP file responds 500 Error. Can’t understand what is the problem. The PHP error message is:
Fatal error: Uncaught exception ‘DOMException’ with message ‘Not Found Error’
Code is below:
$dom = new domDocument;
$dom->preserveWhiteSpace = false;
$dom->loadHTML($cont);
$links = $dom->getElementsByTagName('a');
$images = $dom->getElementsByTagName('img');
$divs = $dom->getElementsByTagName('div');
foreach ($links as $link)
{
$link->setAttribute('target','_blank');
}
foreach ($images as $image)
{
$image->setAttribute('style','max-width:405px');
}
foreach ($divs as $div)
{
$pnode = $dom->createElement('p', $div->nodeValue);
$dom->replaceChild($pnode, $div); //the problem string
}
Update: If the problem string is commented there are no errors.
Thanks to air4x.
replaceChildren looks up through only one level. In my sutuation it was looking for element in the and root.
Solution is below: