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