How PHP implements Transcoding (supports array type transcoding)

Source: Internet
Author: User

Because the Json_encode () method that converts an array to JSON can only convert the Utf-8 character set, the Chinese characters in the UTF-8 format will be null, and the character in the array needs to be uniformly transcoded, but the Iconv () method can only convert string type encoding , the following methods can be found on the Internet to solve the problem of transcoding of string type and array type uniformly

function Auto_charset ($fContents, $from = ' GBK ', $to = ' utf-8 ') {
$from = Strtoupper ($from) = = ' UTF8 '? ' Utf-8 ': $from;
$to = Strtoupper ($to) = = ' UTF8 '? ' Utf-8 ': $to;
if (Strtoupper ($from) = = = Strtoupper ($to) | | empty ($fContents) | | (Is_scalar ($fContents) &&!is_string ($fContents))) {
return $fContents;
}
if (is_string ($fContents)) {
if (function_exists (' mb_convert_encoding ')) {
Return mb_convert_encoding ($fContents, $to, $from);
} else if (function_exists (' Iconv ')) {
Return Iconv ($from, $to, $fContents);
} else {
return $fContents;
}
} else if (Is_array ($fContents)) {
foreach ($fContents as $key = = $val) {
$_key = Auto_charset ($key, $from, $to);
$fContents [$_key] = Auto_charset ($val, $from, $to);
if ($key! = $_key)
Unset ($fContents [$key]);
}
return $fContents;
}
else {
return $fContents;
}
}

How PHP implements Transcoding (supports array type transcoding)

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.