Search code examples
phpxmlsymbols

How do I troubleshoot "simplexml_load_file() parser error: Entity 'nbsp' not defined"?


I use PHP to generate XML files. I have use some code below to avoid error.

$str = str_ireplace(array('<','>','&','\'','"'),array('&lt;','&gt;','&amp;','&apos;','&quot;'),$str);

but still cause fault.

simplexml_load_file() [function.simplexml-load-file] *[file name]* parser error : Entity 'nbsp' not defined in *[file name] [line]*

The error text here:

Dallas&nbsp;&nbsp;Dallas () is the third-largest city in Texas and the ninth-largest in the United States.

In IE8, it seems to fault in (). So how many symbols should I notice?


Solution

  • &nbsp; is a HTML entity, but doesn't exist in XML.

    Either get rid of it (you're not saying where it comes from, so it's hard to give any more specific advice), or wrap your HTML data in CDATA blocks so the parser ignores them.