Search code examples
phpdomdomdocument

PHP DOMDocument error Entity 'nbsp' not defined


I use DOMDocument for editing some HTML files, but some of theme have in their names spaces. So DOMDocument automaticly change the spaces to %20 and then can't find them.

This is how looks the error exactly:

Warning: DOMDocument::load() [domdocument.load]: Entity 'nbsp' not defined in file:///C:/Path/To/The/File/01%20c%2040-1964.html, line: 11 in C:/Path/To/class.php on line 51

How to repair this error?


Solution

  • Use DOMDocument::loadHTMLFile() instead of load(). That's what it has been made for. HTML is not XML.

    XML does not know the named entity  . However if you use loadHTML, the XML parser will get the HTML named entities loaded so the error goes away.

    See as well: XML parser error: entity not defined.