mb_detect_encoding() 是用作检查字串编码的 PHP 函式, 使用上很方便, 例如:
|
1 2 3 4 |
<?php $str = "中文测试"; echo mb_detect_encoding($str); ?> |
以上程式码会印出 $str 的编码, 例如 UTF-8。
iconv() 则是用作转换字串编码的函式, 例如我想将 BIG5 编码转到 UTF-8, 是这样写:
|
1 2 3 4 |
<?php $new_str = iconv("BIG5", "UTF-8", "BIG5 文字"); echo $new_str; ?> |
将上面的 mb_detect_encoding() 及 iconv() 配合使用, 可以先得出字串编码, 然后做转换:
|
1 2 3 4 |
<?php $str = "中文测试"; $new_str = iconv(mb_detect_encoding($str), "UTF-8", $str); ?> |