PHP load XML encoding error, "Error:input is not proper UTF-8, indicate encoding! ”

Source: Internet
Author: User

When parsing XML in PHP recently, an error was thrown:

"Warning: Domdocument::load (): Input is not proper UTF-8, indicate encoding! BYTES:0XBB 0xb6 0xd3 0xAD in File:/e:/phpwork/shopproject/sendcms.xml, line:19 ine:\phpwork\shopproject\xml.php on line 4"

This means that DOMDocument's load function was in error at load, and then I looked at my XML file as follows

1 <?XML version= "1.0"?>2 < Package>3 <Header>4 <Roadno>12345</Roadno>5 <DataType>48</DataType>6 <Moduleno>12345</Moduleno>7 <SourceIP>127.0.0.1</SourceIP>8 <Sourceport>11000</Sourceport>9 <UserID>96998</UserID>Ten <Recordtime>2014-11-19 08:08:08:111</Recordtime> One <Imperative>1</Imperative> A <ResultFlag= "-1"MSG=" " /> - </Header> - <devicelist> the <DevitemDeviceID= "5100001"/> - <DevitemDeviceID= "5100002"/> - </devicelist> - <Visiondata> + <VisionitemTextInfo= "Welcome Entry"Picno=""FontName= "Song Body"FontSize= "Ten"FontColor= "Yellow"Fontformat= "Centered"Interval= "+"Effect= "1" />  - <VisionitemTextInfo= "slow slowing down."Picno=""FontName= "Song Body"FontSize= "Ten"FontColor= "Yellow"Fontformat= "Centered"Interval= "+"Effect= "1" />  + </Visiondata> A </ Package>
Sendcms.xml

And then found that the type definition does not define encoding, here I guess because there is a Chinese character, so the default is Utf-8 encoding, and then I find the method on the Internet, indeed, in stack over flow found a method, respectively, as follows

Method 1:

Solution: Read the text content, and then encode the text content to convert, using the Iconv function to convert, the specific code is as follows

//Read File contents$myfile=fopen("Sendcms.xml", ' R ');$content=fread($myfile,filesize("Sendcms.xml"));fclose($myfile);$content=Iconv(' UTF-8 ', ' Utf-8//ignore ',$content);$xmldoc-LoadXML ($content );Print $xmldoc->savexml ();
Method 1, using the Iconv function

Overall, this solution is good and can be used.

Original address: Stack OverFlow

PHP load XML encoding error, "Error:input is not proper UTF-8, indicate encoding! ”

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.