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

$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 
+79
php
Nov 06 '09 at 3:40
source share
12 answers

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); 
+138
May 7 '12 at 1:05
source share

I would argue that if you look at the source http://www.somesite.com/ , you will find special characters that have not been converted to HTML. Maybe something like this:

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

Must be

 <a href="/script.php?foo=bar&amp;hello=world">link</a> 
+89
Feb 23 '11 at 1:24
source share
 $dom->@loadHTML($html); 

This is incorrect, use instead:

 @$dom->loadHTML($html); 
+51
Oct. 16 '10 at 5:28
source share

The reason for your fatal error is that the DOMDocument does not have a __toString () method and therefore cannot be echoed.

You may be looking for

 echo $dom->saveHTML(); 
+12
Nov 06 '09 at 3:46
source share

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

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

 $dom->@loadHTML($html); 
+10
Feb 27 '10 at 6:43
source share

Regardless of the echo (which should be replaced by print_r or var_dump), if an exception is thrown, the object should remain empty:

 DOMNodeList Object ( ) 

Decision

  • 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); 
  • Use php entity encoding in markup content, which is the most common source of errors.

+10
Sep 12 2018-11-11T00:
source share

replace simple

 $dom->loadHTML($html); 

with more reliable ...

 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; } 
+8
Sep 16 '14 at 22:32
source share
 $html = file_get_contents("http://www.somesite.com/"); $dom = new DOMDocument(); $dom->loadHTML(htmlspecialchars($html)); echo $dom; 

try it

+6
Nov 22 '17 at 11:19
source share

Another possible solution is

 $sContent = htmlspecialchars($sHTML); $oDom = new DOMDocument(); $oDom->loadHTML($sContent); echo html_entity_decode($oDom->saveHTML()); 
+3
Oct 22 '13 at 18:57
source share

I know this is an old question, but if you ever want to fix the incorrect & &; characters 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]+|[AZ|az|0-9]+);/', $string) === 0) { // If a standard escape cannot be found. $html = substr_replace($html, '&amp;', $positionAmp, 1); // This mean we need to escapa the '&' sign. fixAmps($html, $positionAmp+5); // Recursive call from the new position. } else { fixAmps($html, $positionAmp+1); // Recursive call from the new position. } } } 
+3
Feb 15 '15 at 2:02
source share

This is not always due to the content of the page and may be due to the URL itself .

I recently ran into this error, and a carriage character was returned at the end of the URL. The reason for the existence of this character was an error in splitting URLs.

 $urls_array = explode("\r\n", $urls); 

instead

 $urls_array = explode("\n", $urls); 
+1
May 29 '16 at 10:05
source share

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

0
Jun 21 '19 at 5:38
source share



All Articles