Several common methods of PHP string encoding conversion _php Tutorial

Source: Internet
Author: User
Tags mysql tutorial
Several common methods of PHP tutorial string encoding Conversion

Mb_convert_encoding ()
PHP's internal code conversion function
Version (PHP 4 >= 4.0.6, PHP 5)
This function can convert various encodings to each other

Make a GBK to UTF-8
< PHP
Header ("content-type:text/html; Charset=utf-8 ");
Echo mb_convert_encoding ("You Are my Friend", "UTF-8", "GBK");
?>

One more GB2312 to Big5.
< PHP
Header ("content-type:text/html; Charset=big5 ");
Echo mb_convert_encoding ("You Are my Friend", "Big5", "GB2312");
?>


Iconv ()
PHP Internal code conversion function, IBID.
Because Iconv () is a bug when converting gb2312, this is how to handle

PHP Codeiconv ("UTF-8", "Gb2312//ignore", $str)

Ignore means ignoring errors at the time of conversion and discovering that iconv will go wrong when converting the character "-" to gb2312 if there is no

Ignore parameter, all strings following the character cannot be saved.
Another mb_convert_encoding does not have this bug, so the best way to do this is:
PHP codemb_convert_encoding ($str, "gb2312", "UTF-8");

However, you need to enable the Mbstring extension library first.
You can also set the MySQL Tutorial database tutorial collation to Utf-8 without conversion.
A three-sentence MySQL mantra
SQL Code
SET NAMES UTF8;
Set CHARACTER set UTF8;
SET collation_connection= ' utf8_general_ci ';


Custom Function 1
--------------------------------------------------------------------------------

---
On-line conversion function, will GB2312 conversion, modified to Utf-8 after conversion error, unable to parse Chinese ..... Expect positive

The madman ...
PHP Code function Escape ($str) {
Preg_match_all ("/[x80-xff].| [x01-x7f]+/], $STR, $r);
$ar = $r [0];
foreach ($ar as $k = = $v) {
if (Ord ($v [0]) < 128)
$ar [$k] = Rawurlencode ($v);
Else
$ar [$k] = "%u". Bin2Hex (Iconv ("GB2312", "UCS-2", $v));
}
return join ("", $ar);
}

function Unescape ($STR) {
$str = Rawurldecode ($STR);
Preg_match_all ("/(?:%u.{4}) |. +/", $str, $r);
$ar = $r [0];
foreach ($ar as $k = = $v) {
if (substr ($v, 0,2) = = "%u" && strlen ($v) = = 6)
$ar [$k] = Iconv ("UCS-2", "GB2312", Pack ("H4", substr ($v,-4)));
}
return join ("", $ar);
}
?>


Custom Function 2
--------------------------------------------------------------------------------

---
Thanks to the Forum gingzai777, the master is not the same, a glance can see the problem lies ....
After the PHP filter with this line, do not need to worry about file encoding .....
PHP Code function Addslashes_str ($str) {
$str =addslashes ($STR);
$str =str_replace ($str, ";", '; ');
return $str;
}
function Strips Tutorial Lashes_str ($str) {
$str =stripslashes ($STR);
$str =str_replace ($str, '; ', ";");
return $str;
}
?>

http://www.bkjia.com/PHPjc/632291.html www.bkjia.com true http://www.bkjia.com/PHPjc/632291.html techarticle PHP Tutorial String encoding Conversion of several common methods mb_convert_encoding () PHP Internal Code conversion function version (PHP 4 = 4.0.6, PHP 5) This function can convert various codes to one another ...

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