Turn http://blog.csdn.net/u013372487/article/details/52528535
iconv Detailed:
iconv-strings are converted As required character encodings
Iconv has a bug, encountered some rare words will not be able to convert, of course, when configuring the second parameter, you can make up a little bit of the default flaw, not to be able to convert is truncated, use the following
Iconv ("Utf-8″," Gb2312//ignore ", $data);
When this encounters an uncommon character conversion failure, it ignores the failure and continues to convert the following content.
iconvstring iconv ( string $in_charset , string $out_charset , string $str )第一个参数:内容原的编码第二个参数:目标编码第三个参数:要转的字符串函数返回字符串<?php$instr = ‘测试’;// GBK转UTF-8$outstr = iconv(‘GBK’,‘UTF-8′,$instr);?>
return value
Returns the converted string, or FALSE on failure.
mb_convert_encoding Detailed:
To ensure the success of the conversion, we can use another conversion function
Mb_convert_encoding, this function is not very efficient, and this function can also omit the third parameter, automatically identify the content encoding, but it is best not to use, affect efficiency, but also need to pay attention to the mb_convert_encoding and Iconv parameter order is not the same , be sure to pay attention.
A simple usage of two functions:
mb_convert_encodingstring mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )第一个参数:要处理的字符串第二个参数:目标编码第三个参数:内容原编码,它可以是一个 array 也可以是逗号分隔的枚举列表<?php$instr = ‘测试‘;// GBK转UTF-8$outstr = mb_convert_encoding($instr,‘UTF-8‘,‘GBK‘,);$str = mb_convert_encoding($instr, "UCS-2LE", "JIS, eucjp-win, sjis-win");?>
Personal advice to meet transcoding problems when using mb_convert_encoding comparative insurance.
Mb_convert_variables
mb_convert_variables- converting the character encoding of one or more variables
mb_convert_variables ( $to_encoding , $from_encoding , &$vars [, mixed &$... ] )
Converts the encoding of the variable VARs from from_encoding to the encoded to_encoding.
mb_convert_variables() 会拼接变量数组或对象中的字符串来检测编码,因为短字符串的检测往往会失败。因此,不能在一个数组或对象中混合使用编码。
to_encoding string 转换成这个编码。from_encoding 可以指定为一个 array 或者逗号分隔的 string,它将尝试根据 from-coding 来检测编码。 当省略了 from_encoding,将使用 detect_order。vars 是要转换的变量的引用。 参数可以接受 String、Array 和 Object 的类型。 mb_convert_variables() 假设所有的参数都具有同样的编码。额外的 vars。
FALSE。
实例:<?php/* 转换变量 $post1、$post2 编码为内部(internal)编码 */$interenc = mb_internal_encoding();$inputenc = mb_convert_variables($interenc, "ASCII,UTF-8,SJIS-win", $post1, $post2);?>
mb_internal_encoding
mb_internal_encoding- set/Get internal character encoding
string $encoding = mb_internal_encoding() ] )
TRUE, 或者在失败时返回 FALSE。 In this case, the character encoding for multibyte regex is NOT changed. 如果省略了 encoding,则返回当前的字符编码名称。
<?php/* 设置内部字符编码为 UTF-8 */mb_internal_encoding("UTF-8");/* 显示当前的内部字符编码*/echo mb_internal_encoding();?>
Mb_detect_encoding Detailed:
mb_detect_encoding- encoding of detected characters
string mb_detect_encoding ( string $str [, mixed $encoding_list = mb_detect_order() [, bool $strict = false ]] )
Detects the encoding of the string str.
参数 str 待检查的字符串。encoding_list 是一个字符编码列表。 编码顺序可以由数组或者逗号分隔的列表字符串指定。如果省略了 encoding_list 将会使用 detect_order。strict strict 指定了是否严格地检测编码。 默认是 FALSE。返回值检测到的字符编码,或者无法检测指定字符串的编码时返回 FALSE。
encode a string when the string encoding is unknown:
1, regardless of the string encoding is what, convert to GBK
function getSafeStr($str){ $s1 = iconv(‘utf-8‘,‘gbk//IGNORE‘,$str); $s0 = iconv(‘gbk‘,‘utf-8//IGNORE‘,$s1); if($s0 == $str){ return $s1; }else{ return $str; }}
2, regardless of the string encoding is what, convert to Utf-8
function getSafeStr($str){ $s1 = iconv(‘gbk‘,‘utf-8//IGNORE‘,$str); $s0 = iconv(‘utf-8‘,‘gbk//IGNORE‘,$s1); if($s0 == $str){ return $s1; }else{ return $str; }}
Get the string encoding method:
function getcode($str){ $s1 = iconv(‘utf-8‘,‘gbk//IGNORE‘,$str); $s0 = iconv(‘gbk‘,‘utf-8//IGNORE‘,$s1); if($s0 == $str){ return ‘utf-8‘; }else{ return ‘gbk‘; }}
PHP Encoding Conversion garbled solution