Php string encoding and conversion methods. Php Tutorial: several common methods for string encoding conversion: mb_convert_encoding () PHP internal code conversion function version (PHP44.0.6, PHP5) this function can convert various encodings into one another. in php Tutorial, there are several common methods for string encoding conversion.
Mb_convert_encoding ()
PHP 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 friends", "UTF-8", "GBK ");
?>
Another 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, same as above
Because iconv () is a bug in gb2312 conversion, it should be handled like this
PHP codeiconv ("UTF-8", "gb2312 // IGNORE", $ str)
Ignore indicates that the conversion error is ignored. it is found that iconv will encounter an error when converting the character "-" to gb2312. If no
Ignore parameter. all strings after this character cannot be saved.
In addition, mb_convert_encoding does not have this bug, so the best syntax is:
PHP codemb_convert_encoding ($ str, "gb2312", "UTF-8 ");
However, enable mbstring Extension Library is required first.
You can also set the collation of the mysql tutorial database tutorial to UTF-8, which is not used for conversion.
Mysql statement
SQL code
Set names utf8;
Set character set utf8;
SET COLLATION_CONNECTION = 'utf8 _ general_ci ';
Custom Function 1
--------------------------------------------------------------------------------
---
If the conversion function found on the Internet converts GB2312 to UTF-8, the conversion error is returned. Chinese characters cannot be parsed.
Crazy man...
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 gingzai777 Forum, the experts are different. you can see the problem at a glance .....
This line will be used for php filtering in the future, so you don't 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;
}
?>
Several common methods for converting strings in sequence: mb_convert_encoding () PHP internal code conversion function version (PHP 4 = 4.0.6, PHP 5). This function can convert various encodings into one another...