Solve the problem that PHP loadxml () cannot recognize spaces.

Source: Internet
Author: User
Tags processing instruction
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.