//检查字符串的编码
mb_detect_encoding — 检测字符的编码
说明
$str
[,
mixed $encoding_list
= mb_detect_order() [,
bool $strict
= false ]] )
检测字符串 str
的编码。
参数
-
待检查的字符串。
-
encoding_list
是一个字符编码列表。 编码顺序可以由数组或者逗号分隔的列表字符串指定。如果省略了
encoding_list
将会使用 detect_order。 -
strict
指定了是否严格地检测编码。 默认是FALSE
。
str
encoding_list
strict
返回值
检测到的字符编码,或者无法检测指定字符串的编码时返回 FALSE
。
实例
$encode = mb_detect_encoding($str,array("ASCII","UTF-8","GB2312","GBK"));
echo $encode; //返回$str字符窜的编码
//转换字符串编码
mb_convert_encoding — 转换字符的编码
说明
$str
, string $to_encoding
[, mixed $from_encoding
] )
将 string 类型 str
的字符编码从可选的 from_encoding
转换到 to_encoding
。
参数
-
要编码的 string。
-
str
要转换成的编码类型。 -
在转换前通过字符代码名称来指定。它可以是一个 array 也可以是逗号分隔的枚举列表。 如果没有提供
from_encoding
,则会使用内部(internal)编码。
str
to_encoding
from_encoding
返回值
编码后的 string。
实例
<?php
/* 转换内部编码为 SJIS */
$str = mb_convert_encoding($str, "SJIS");
/* 将 EUC-JP 转换成 UTF-7 */
$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");
/* 从 JIS, eucjp-win, sjis-win 中自动检测编码,并转换 str 到 UCS-2LE */
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");
/* "auto" 扩展成 "ASCII,JIS,UTF-8,EUC-JP,SJIS" */
$str = mb_convert_encoding($str, "EUC-JP", "auto");
?>