Chinese characters in the PHP application often brings us some trouble, today found an array of arrays on the Internet to convert to XML found that the Chinese characters are empty, and then GG're wrong about a better result, the following share with you.
In PHP array to XML we will write this in the PHP middle School
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{ $xml->addchild ($key, $value); } } return $xml->asxml (); }
Header (' Content-type:text/xml '); Print Array2xml ($array);
|
When the content appears empty when the Chinese characters appear
The solution is to go to the coding process
|
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 if a Chinese character appears } $xml->addchild ($key, $value); } } return $xml->asxml (); } |
Back to everyone else on the Chinese regular example
1. Determine if the strings are all Chinese characters
The code is as follows |
Copy Code |
$str = ' All are kanji tests '; if (Preg_match_all ("/^ ([X81-xfe][x40-xfe]) +$/", $str, $match)) { Echo ' All is Kanji '; } else { Echo ' is not all Chinese characters '; } ?> |
When $str = ' All is a kanji test '; When the output is "all Chinese characters";
When $str = ' All is a kanji test '; When the output is "not all Chinese characters";
http://www.bkjia.com/PHPjc/633188.html www.bkjia.com true http://www.bkjia.com/PHPjc/633188.html techarticle Chinese characters in the PHP application often brings us some trouble, today found an array of arrays on the Internet to convert to XML found that the Chinese characters are empty, and then GG're wrong about to come to a better result ...