Chinese characters often bring us some troubles in PHP applications. Today when I found an array on the Internet and converted it into xml, I found that the Chinese characters were empty. Later, I gged Guan Tian and got better results. The following is the same as Share it with everyone.
When converting php array to xml, we learned how to write it in php
The code is as follows
代码如下 |
复制代码 |
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
header('Content-type: text/xml');
print array2xml($array);
|
|
Copy code
|
function array2xml($array, $xml = false){
If($xml === false){
代码如下 |
复制代码 |
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
//$value=utf8_encode($value);
if (preg_match("/([x81-xfe][x40-xfe])/", $value, $match)) {
$value = iconv('gbk', 'utf-8', $value);
//判断是否有汉字出现
}
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
|
$xml = new SimpleXMLElement('');
}
foreach($array as $key => $value){
If(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
$xml->addChild($key, $value);
代码如下 |
复制代码 |
$str = '全部是汉字测试';
if (preg_match_all("/^([x81-xfe][x40-xfe])+$/", $str, $match)) {
echo '全部是汉字';
} else {
echo '不全是汉字';
}
?>
|
}
}
Return $xml->asXML();
} |
header('Content-type: text/xml');
print array2xml($array);
When Chinese characters appear in the content, it will be empty
The solution is to transcode
The code is as follows
|
Copy code
|
function array2xml($array, $xml = false){
If($xml === false){
$xml = new SimpleXMLElement('');
}
foreach($array as $key => $value){
If(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
//$value=utf8_encode($value);
If (preg_match("/([x81-xfe][x40-xfe])/", $value, $match)) {
$value = iconv('gbk', 'utf-8', $value);
//Determine whether any Chinese characters appear
}
$xml->addChild($key, $value);
}
}
Return $xml->asXML();
}
I will give you some other examples of Chinese character regularization later
1. Determine whether the string is all Chinese characters
The code is as follows
|
Copy code
|
$str = 'All are Chinese character tests';<🎜>
If (preg_match_all("/^([x81-xfe][x40-xfe])+$/", $str, $match)) {<🎜>
echo 'All are Chinese characters'; <🎜>
} else {<🎜>
echo 'Not all Chinese characters';<🎜>
}<🎜>
?>
When $str = 'All are Chinese characters test'; output "All are Chinese characters";
When $str = 'all are Chinese characters test'; output "not all Chinese characters";
http://www.bkjia.com/PHPjc/633188.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633188.htmlTechArticleChinese characters often bring us some trouble in PHP applications. Today I found an array array on the Internet and converted it into xml. At that time, I found that the Chinese characters were empty, and then I gged Guan Tian and got better results...
|
|