SimpleXMLElement object character encoding in PHP

Source: Internet
Author: User
Tags addchild

You have recently used simplexmlelement to generate and parse XML.

Because we use PHP to develop this side using UTF-8 encoding, and the other side using GBK encoding, so encountered the Chinese character encoding problem.

It was later found that the internal encoding of XML and its head

<?xml version="1.0" encoding="gbk" ?>

There is an absolute relationship between the encoding declared in the UTF-8 encoded Chinese characters written to the object, the string returned by the Asxml method is GBK, no conversion is required, and vice versa.

Recommended children's shoes for working with XML in PHP use the SimpleXMLElement object, do not connect the string directly

Cases:

$STRXML ='<?xml version= "1.0" encoding= "GBK"?><SnsPropSet></SnsPropSet>'; $OBJXML =simplexml_load_string ($STRXML); $OBJXML->addattribute ('Snscid', $ This-intcid); $OBJXML->addattribute ('Snstid', $ This-Inttid); $OBJXML->addattribute ('Snspid', $ This-intpid); $intCount= $strCount =0; if(Is_int ($mixSnsAttrVal)) {$objXmlChild= $objXml->addchild ('Propint'); $intCount++; } Else{$objXmlChild= $objXml->addchild ('Propdyn'); $strCount++; } $objXmlChild->addattribute ('ID', $intSnsAttrId); $objXmlChild->addattribute ('V', $mixSnsAttrVal); $objXml->addattribute ('PROPINTC', $intCount); $OBJXML->addattribute ('Propdync', $strCount); $STRXML= $objXml->asxml ();

SimpleXMLElement object character encoding in PHP

Related Article

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.