Georgy Liparteliani
Georgy Liparteliani

Reputation: 403

DOMDocument::replaceChild() responds results in 500 (Internal Server error)

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.

Upvotes: 0

Views: 1649

Answers (2)

Georgy Liparteliani
Georgy Liparteliani

Reputation: 403

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:

 $div->parentNode->replaceChild($pnode, $div);

Upvotes: 1

Michal Schwarz
Michal Schwarz

Reputation: 72

The code seems to be OK. Check if you have a DOM/XML php extension enabled in phpinfo();. Add

ini_set('display_errors', 1);
error_reporting(E_ALL);

to your code or enable php error showing in .htaccess.

Upvotes: 1

Related Questions