Phpsimplexml_load_file Chinese garbled solution _ PHP Tutorial

Source: Internet
Author: User
Phpsimplexml_load_file. Most of Chinese garbled characters are caused by encoding. we only need to adjust the output encoding to solve the Chinese garbled characters problem. The following describes how to solve simplexml_load_file Chinese garbled characters. In PHP5.0, most of Chinese garbled characters are produced by encoding. we only need to adjust the output encoding to solve the Chinese garbled text problem. The following describes how to solve the Chinese garbled characters in simplexml_load_file.

In PHP5.0, simplexml_load_file () is a good function for reading and processing XML files, but garbled characters occur when reading and processing Chinese files. after research, it is found that if it is Chinese content, it must be converted by iconv encoding; otherwise, it is garbled.

The code is as follows:

$ Xml = simple_load_file ('XML file ');

Foreach ($ xml-> soft as $ temp ){

Echo $ temp-> name; // This is the software name.

Echo $ temp-> mem; // This is the software description.

}

Correct English and Chinese characters are garbled. I guess this is the encoding problem.

Later, I found related articles on the Internet, such as the core sentence.

1. if the xml encoding does not match the file type, an error is reported immediately.
2. simplexml can process gbk XML, that is, the file content and encoding are both gbk (ANSI format)
3. no matter what the file content is, the processing is finished, and it is UTF8.


The specific method is as follows:

The code is as follows:

$ Chname = iconv ('utf-8', 'gb2312', $ table-> param ["name"]);

Convert the content of the XML file encoded as 'utf-8' to the content in gb2312 format.

The Chinese encoding format is gb2312.

There is also a way I have not tested the code of the direct vendors page to uft8

The code is as follows:

Header ("Content-type: text/html; charset = utf-8 ");
// Because wordpress uses UTF-8 encoding by default, but IE recognizes it as gb2312 by default, use the header to issue the encoding.
$ Url = "http://www.bKjia. c0m ";
$ Xml = simplexml_load_file ($ url );
// Load the remote XML file
$ Title = $ xml-> channel-> title;
// Obtain the blog title based on the node (not repeating)
Echo $ title ."
";
// If the node loops, you can use the foreach loop to retrieve
Foreach ($ xml-> channel-> item as $ item ){
Echo $ item-> title ."
";
}
// Cyclically retrieve the article title

Bytes. In PHP5.0 ,...

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.