PHP Encoding conversion function automatic conversion character set support array conversion _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
Automatic conversion character set supports array conversion
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))) {
If the encoding is the same or the non-string scalar is not converted
return $fContents;
}
if (is_string ($fContents)) {
if (function_exists (' mb_convert_encoding ')) {
Return mb_convert_encoding ($fContents, $to, $from);
} elseif (Function_exists (' Iconv ')) {
Return Iconv ($from, $to, $fContents);
} else {
return $fContents;
}
} elseif (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;
}
}

When we accept the data submitted by unknown clients, because the encoding of each client is not uniform, but in our server end can only be processed in one encoding, in this case, it will involve the conversion of the accepted character to a specific encoding problem.
You might think of transcoding directly with Iconv, but we know that the function needs to provide two parameters for the input encoding and output encoding, and we don't know what the string is to be encoded at this time, if we can get the code of the received character.
For such a problem, there are generally two kinds of solutions.

programme I
To specify the encoding to be submitted when the client submits the data, it is necessary to give more than one variable to specify the encoding.
$string = $_get[' charset '] = = = ' GBK '? Iconv (' GBK ', ' utf-8 ', $_get[' str '): $_get[' str '];
In this case, if there is no agreement or we cannot control the client, it seems that this scenario is not used very well.

Scenario Two
The data encoding received is detected directly by the server side.
This scheme is certainly the most ideal, and now the question is how to detect the encoding of a character? In this case, in PHP, Mb_string's mb_check_encoding in this extension provides the functionality we need.
$str = mb_check_encoding ($_get[' str '), ' GBK ')? Iconv (' GBK ', ' utf-8 ', $_get[' str '): $_get[' str '];
But this need to open mb_string this extension, and sometimes it may not be open in our production server. In this case, you need to use the following function to determine the encoding.
The following function is not written by me
Copy CodeThe code is as follows:
function isGb2312 ($string) {
for ($i =0; $i 127) {
if (($v >= 228) && ($v < = 233))
{
if ($i +2) >= (strlen ($string)-1)) return true;
$v 1 = ord ($string [$i +1]);
$v 2 = Ord ($string [$i +2]);
if (($v 1 >=) && ($v 1 < =191) && ($v 2 >=128) && ($v 2 < = 191))
return false;
Else
return true;
}
}
}
return true;
}
function IsUtf8 ($string) {
Return Preg_match ('%^ (?:
[\x09\x0a\x0d\x20-\x7e] # ASCII
| [\XC2-\XDF] [\X80-\XBF] # Non-overlong 2-byte
| \XE0[\XA0-\XBF][\X80-\XBF] # excluding overlongs
| [\xe1-\xec\xee\xef] [\X80-\XBF] {2} # straight 3-byte
| \XED[\X80-\X9F][\X80-\XBF] # excluding surrogates
| \XF0[\X90-\XBF][\X80-\XBF]{2} # Planes 1-3
| [\xf1-\xf3] [\X80-\XBF] {3} # planes 4-15
| \XF4[\X80-\X8F][\X80-\XBF]{2} # Plane 16
) *$%xs ', $string);
}

Here we can make any of the above functions to implement the code detection. and converts it to the specified encoding.
$str = isGb2312 ($_get[' str '), ' GBK ')? Iconv (' GBK ', ' utf-8 ', $_get[' str '): $_get[' str '];

http://www.bkjia.com/PHPjc/326216.html www.bkjia.com true http://www.bkjia.com/PHPjc/326216.html techarticle Copy the code as follows://automatic conversion character set supports array conversion function Auto_charset ($fContents, $from = ' GBK ', $to = ' utf-8 ') {$from = Strtoupper ($from) = = ' UTF8 '? ' Utf-8 ': ...

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