Simplexml_load_string parsing GB2312 encoded XML

Source: Internet
Author: User

<?phpheader (' Content-type:text/html;charset=utf-8 '); $url = ' http://www.xxx.com/text.xml '; $content = Str_replace (' gb2312 ', ' UTF-8 ', file_get_contents ($url)); $content = Iconv (' GBK ', ' UTF-8 ', $content);//$content = Utf8_encode ($ content);//Print_r ($content);//echo "<br/>************<br/>"; try{$data = simplexml_load_string ($ Content) or Die (' Unable to load XML ');} catch (Excetion $e) {print_r ($e->getmessage ());} if (Isset ($data->item)) {echo "ok<br/>";} Else{echo "Error<br/>";} echo "<br/>************<br/>";p rint_r ($data);//$content = Sxe ($url);//Var_dump ($content); function Sxe (    $url) {$xml = file_get_contents ($url); foreach ($http _response_header as $header) {if (Preg_match (' #^content-type:text/xml; charset= (. *) #i ', $head                    Er, $m)) {switch (Strtolower ($m [1])) {case ' utf-8 ':                Do nothing break;       Case ' iso-8859-1 ':             $xml = Utf8_encode ($xml);                Break            Default: $xml = Iconv ($m [1], ' utf-8 ', $xml);        } break; }} return Simplexml_load_string ($xml);}

  

Reference:

Http://stackoverflow.com/questions/2899274/php-simplexml-why-does-simplexml-load-string-fail-to-parse-google-weather

http://bbs.csdn.net/topics/390070967#post-391437130

http://php.net/manual/zh/function.iconv.php

Http://zhidao.baidu.com/question/157551132.html?fr=iks&word=php+%BD%E2%CE%F6xml%C2%D2%C2%EB&ie=gbk

Simplexml_load_string parsing GB2312 encoded XML

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.