Source: http://phperwuhan.blog.163.com/blog/static/4114260220083945114254/
When using loadxml () to parse a string that contains entity references (e.g ., & nbsp;), be sure that those entity references are properly declared through the use of a doctype Declaration; otherwise, loadxml () will not be able to interpret the string.
Example:
<? PHP
$ STR = <XML
<? XML version = "1.0" encoding = "iso-8859-1"?>
<Div> This & nbsp; is a non-breaking space. </div>
XML;
$ DD1 = new domdocument ();
$ DD1-> loadxml ($ Str );
Echo $ DD1-> savexml ();
?>
Given the above Code, PHP will issue a warning about the entity 'nbsp 'not being properly declared. also, the call to savexml () will return nothing but a trimmed-down version of the original processing instruction... everything else is gone, and all because of the undeclared entity.
Instead, explicitly declare the entity first:
<? PHP
$ STR = <XML
<? XML version = "1.0" encoding = "iso-8859-1"?>
<! Doctype root [
<! Entity nbsp "& #160;">
]>
<Div> This & nbsp; is a non-breaking space. </div>
XML;
$ Dd2 = new domdocument ();
$ Dd2-> loadxml ($ Str );
Echo $ dd2-> savexml ();
?>
Since the 'nbsp 'entity is defined in the doctype, PHP no longer issues that warning; the string is now well-formed, and loadxml () understands it perfectly.
You can also use references to external dtds in the same way (e.g., <! Doctype HTML public "-// W3C // dtd html 4.01 // en"
"Http://www.w3.org/TR/html4/strict.dtd">), which is particle ly important if you need to do this for your different events with your different possible entities.
Also, as a Sidenote... entity references created by createentityreference () do not need this kind of explicit declaration