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 ,...