1.查看页面输入,内部,输出编码:
print_r(iconv_get_encoding("all"));
2.对指字符串进行编码转换:
echo iconv('utf-8','gb2312','我们'); //把‘我们’由utf8编码转换为gb2312编码
**iconv(in_charset,outcharset//TRANSLIT//IGNORE,$string);//'TRANSLIT':如果在输出的编码格式中不含有的字符,可以在类似编码中查找取代;'IGNORE':如果输出格式编码中不含有字符串中的某字符时,能够跳过进行后面字符的编码。否则在转码失败处会中断输出,产生错误。
3.将字符串进行编码转换(可自动判断编码类型,但听说效率没有iconv好。)
mb_convert_encoding(‘我们’, 'utf-8','gb2312' ); //将‘我们’由gb2312转换为utf8
mb_convert_encoding(‘我们’, 'utf-8'); //将‘我们’转换成utf8编码格式
/* "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */
$str = mb_convert_encoding($str, "EUC-JP", "auto");
/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");
**第三个参数还可以是array()形式
4.设置编码格式:
iconv_set_encoding("internal_encoding", "UTF-8"); //设置内部编码为utf8
iconv_set_encoding("output_encoding", "ISO-8859-1"); //设置输出编码为ISO-8859-1
设置选项:1.input_encoding 2.output_encoding 3.internal_encoding
**ISO-8859-1编码是单字节编码,向下兼容ASCII, Latin1是ISO-8859-1的别名
5.查看字符串编码方式:
格式:string mb_detect_encoding(string$str[,mixed$encoding_list= mb_detect_order()[,bool$strict= false]] )
$str='编码方式';
echo mb_detect_encoding($str); //:UTF-8
/* "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */
echo mb_detect_encoding($str, "auto");
/* Specify encoding_list character encoding by comma separated list */
echo mb_detect_encoding($str, "JIS, eucjp-win, sjis-win");
/* Use array to specify encoding_list */
$ary[] = "ASCII";
$ary[] = "JIS";
$ary[] = "EUC-JP";
echo mb_detect_encoding($str, $ary);
6.查看文件编码方式:
$file = 'text3.txt';
echo getFileEncoding(file_get_contents($file)); // 输出UTF-16LE
7.判断字符串是否符合指定格式编码:
格式:bool mb_check_encoding([string$var=NULL[,string$encoding= mb_internal_encoding()]] )
$string="\x00\x81";
$encoding="Shift_JIS";
mb_check_encoding($string,$encoding) //:true
8.单个或多个变量的字符编码转换:
格式:stringmb_convert_variables(string$to_encoding,mixed$from_encoding,mixed&$vars[,mixed&$...] )
**$from_encoding:可以是数字形式,用逗号分隔字符串或结构体形式。
$str1= '测试编码';$str21= '测试编码2';
$inputenc = mb_convert_variables("UTF-8", "UTF-8,GBK,GB2312", $str1, $str2);
var_dump($inputenc); //: string(5) "UTF-8"
var_dump($str1); //: string(12) "测试编码"
**CP936是GBK
文章来源:http://www.cnblogs.com/gaoshicai/archive/2012/06/14/2548976.html