Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,

Php

Php Problem Overview


$html = file_get_contents("http://www.somesite.com/");

$dom = new DOMDocument();
$dom->loadHTML($html);

echo $dom;

throws

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,
Catchable fatal error: Object of class DOMDocument could not be converted to string in test.php on line 10

Php Solutions


Solution 1 - Php

To evaporate the warning, you can use libxml_use_internal_errors(true)

// create new DOMDocument
$document = new \DOMDocument('1.0', 'UTF-8');

// set error level
$internalErrors = libxml_use_internal_errors(true);

// load HTML
$document->loadHTML($html);

// Restore error level
libxml_use_internal_errors($internalErrors);

Solution 2 - Php

I would bet that if you looked at the source of http://www.somesite.com/ you would find special characters that haven't been converted to HTML. Maybe something like this:

<a href="/script.php?foo=bar&hello=world">link</a>

Should be

<a href="/script.php?foo=bar&amp;hello=world">link</a>

Solution 3 - Php

$dom->@loadHTML($html);

This is incorrect, use this instead:

@$dom->loadHTML($html);

Solution 4 - Php

There are 2 errors: the second is because $dom is no string but an object and thus cannot be "echoed". The first error is a warning from loadHTML, caused by invalid syntax of the html document to load (probably an & (ampersand) used as parameter separator and not masked as entity with &).

You ignore and supress this error message (not the error, just the message!) by calling the function with the error control operator "@" (http://www.php.net/manual/en/language.operators.errorcontrol.php )

@$dom->loadHTML($html);

Solution 5 - Php

The reason for your fatal error is DOMDocument does not have a __toString() method and thus can not be echo'ed.

You're probably looking for

echo $dom->saveHTML();

Solution 6 - Php

Regardless of the echo (which would need to be replaced with print_r or var_dump), if an exception is thrown the object should stay empty:

DOMNodeList Object
(
)

Solution

  1. Set recover to true, and strictErrorChecking to false

     $content = file_get_contents($url);
    
     $doc = new DOMDocument();
     $doc->recover = true;
     $doc->strictErrorChecking = false;
     $doc->loadHTML($content);
    
  2. Use php's entity-encoding on the markup's contents, which is a most common error source.

Solution 7 - Php

replace the simple

$dom->loadHTML($html);

with the more robust ...

libxml_use_internal_errors(true);

if (!$DOM->loadHTML($page))
    {
        $errors="";
        foreach (libxml_get_errors() as $error)  {
            $errors.=$error->message."<br/>";
        }
        libxml_clear_errors();
        print "libxml errors:<br>$errors";
        return;
    }

Solution 8 - Php

$html = file_get_contents("http://www.somesite.com/");

$dom = new DOMDocument();
$dom->loadHTML(htmlspecialchars($html));

echo $dom;

try this

Solution 9 - Php

I know this is an old question, but if you ever want ot fix the malformed '&' signs in your HTML. You can use code similar to this:

$page = file_get_contents('http://www.example.com');
$page = preg_replace('/\s+/', ' ', trim($page));
fixAmps($page, 0);
$dom->loadHTML($page);


function fixAmps(&$html, $offset) {
    $positionAmp = strpos($html, '&', $offset);
    $positionSemiColumn = strpos($html, ';', $positionAmp+1);

    $string = substr($html, $positionAmp, $positionSemiColumn-$positionAmp+1);

    if ($positionAmp !== false) { // If an '&' can be found.
        if ($positionSemiColumn === false) { // If no ';' can be found.
            $html = substr_replace($html, '&amp;', $positionAmp, 1); // Replace straight away.
        } else if (preg_match('/&(#[0-9]+|[A-Z|a-z|0-9]+);/', $string) === 0) { // If a standard escape cannot be found.
            $html = substr_replace($html, '&amp;', $positionAmp, 1); // This mean we need to escape the '&' sign.
            fixAmps($html, $positionAmp+5); // Recursive call from the new position.
        } else {
            fixAmps($html, $positionAmp+1); // Recursive call from the new position.
        }
    }
}

Solution 10 - Php

Another possibile solution is

$sContent = htmlspecialchars($sHTML);
$oDom = new DOMDocument();
$oDom->loadHTML($sContent);
echo html_entity_decode($oDom->saveHTML());

Solution 11 - Php

Another possibile solution is,maybe your file is ASCII type file,just change the type of your files.

Solution 12 - Php

Even after this my code is working fine , so i just removed all warning messages with this statement at line 1 .

<?php error_reporting(E_ERROR); ?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongwegView Question on Stackoverflow
Solution 1 - PhpDewsworldView Answer on Stackoverflow
Solution 2 - PhpmattalxndrView Answer on Stackoverflow
Solution 3 - PhpMaanas RoyyView Answer on Stackoverflow
Solution 4 - Phpuser279583View Answer on Stackoverflow
Solution 5 - PhpMike BView Answer on Stackoverflow
Solution 6 - PhpLorenz Lo SauerView Answer on Stackoverflow
Solution 7 - PhpDavid ChanView Answer on Stackoverflow
Solution 8 - Phpnmwi22View Answer on Stackoverflow
Solution 9 - PhpNicolas BouvretteView Answer on Stackoverflow
Solution 10 - PhplastYorshView Answer on Stackoverflow
Solution 11 - PhpFRANKView Answer on Stackoverflow
Solution 12 - PhpStephenView Answer on Stackoverflow