Home > Backend Development > PHP Tutorial > php simplexml_load_file Chinese garbled solution_PHP tutorial

php simplexml_load_file Chinese garbled solution_PHP tutorial

WBOY
Release: 2016-07-13 10:57:31
Original
1399 people have browsed it

Chinese garbled characters are mostly caused by encoding. We only need to adjust the output encoding to solve the problem of Chinese garbled characters. Let’s look at the solution to the Chinese garbled characters of simplexml_load_file.

In PHP5.0, simplexml_load_file() is a good function for reading and processing XML files, but garbled characters will appear when reading and processing Chinese. After research, it was found that if it is Chinese content, it must go through iconv Encoding conversion, otherwise it will be displayed as garbled characters.

3. No matter what the file content is, after processing, it will be UTF8.
The code is as follows
 代码如下 复制代码

$xml=simple_load_file('xml文件');

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

  echo $temp->name; //这就是软件名称了。 

  echo $temp->mem; //这就是软件说明了。

}

Copy code

$xml=simple_load_file('xml file');

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

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

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

}
 代码如下 复制代码

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

English is correct and Chinese is garbled. I guess this is an encoding problem.

Later I found related articles online, such as the core sentence

1. If the encoding of xml does not match the file type, an error will be reported immediately
 代码如下 复制代码

header("Content-type: text/html; charset=utf-8");
//因为wordpress默认编码是utf-8,但IE默认识别为gb2312,所以用header发一下编码
$url="http://www.bKjia.c0m";
$xml =  simplexml_load_file($url);
//载入远程XML文件
$title=$xml->channel->title;
//根据节点获取博客标题(不循环)
echo $title."
";
//如果节点循环了,可以用foreach循环取出
foreach($xml->channel->item as $item){
 echo $item->title."
";
}
//循环取出文章标题

2. simplexml can process gbk’s XML, that is, the file content and encoding are both gbk (ANSI format)
The specific method is as follows:

The code is as follows
Copy code
$chname = iconv('utf-8', 'gb2312', $table->param["name"]); Convert XML file content encoded as 'utf-8' to gb2312 format content. Chinese encoding is output in gb2312 format. There is another way, which I haven’t tested yet, to directly convert the vendor page encoding to uft8
The code is as follows
Copy code
header("Content-type: text/html; charset=utf-8"); //Because the default encoding of wordpress is utf-8, but IE recognizes it as gb2312 by default, so use the header to send the encoding $url="http://www.bKjia.c0m"; $xml = simplexml_load_file($url); //Load remote XML file $title=$xml->channel->title; //Get the blog title based on the node (without looping) echo $title."
"; //If the node is looped, you can use foreach loop to take it out foreach($xml->channel->item as $item){ echo $item->title."
"; } //Loop out the article title http://www.bkjia.com/PHPjc/632066.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632066.htmlTechArticleChinese garbled characters are mostly caused by encoding. We only need to adjust the output encoding to solve the Chinese garbled problem. Let's look at simplexml_load_file Solution to Chinese garbled characters. In PHP5.0,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template