Troubleshooting PHP array arrays generating XML files Chinese character coding _php Tutorial

Source: Internet
Author: User
Tags addchild
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 ...

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