用iconv函數:
iconv( "utf-8", "gb2312" , "abc陽光123");
據說這樣好些,可以忽略轉換時的錯誤!
iconv( "utf-8", "gb2312//ignore" , "abc陽光123");
mb_convert_encoding的用法見官方:
http://cn.php.net/manual/zh/function.mb-convert-encoding.php
做一個gbk to utf-8
- <?php
- header("content-type: text/html; charset=utf-8");
- echo mb_convert_encoding("我的友仔", "utf-8", "gbk");
- ?>
再來個gb2312 to big5
- <?php
- header("content-type: text/html; charset=big5");
- echo mb_convert_encoding("你是我的朋友", "big5", "gb2312");
- ?>
不過要使用上面的函數需要安裝但是需要先安裝 enable mbstring 擴展庫。php中的另外一個函數iconv也是用來轉換字符串編碼的,與上函數功能相似。
下面還有一些詳細的例子:
iconv — convert string to requested character encoding(php 4 >= 4.0.5, php 5)
mb_convert_encoding — convert character encoding(php 4 >= 4.0.6, php 5)
用法:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
需要先enable mbstring 擴展庫,在 php.ini里將; extension=php_mbstring.dll 前面的 ; 去掉
mb_convert_encoding 可以指定多種輸入編碼,它會根據內容自動識別,但是執行效率比iconv差太多;
|
新聞熱點
疑難解答