php中mb_convert_encoding 和 iconv 的區(qū)別_PHP教程
推薦:php拾遺//計(jì)算庫(kù)存 foreach ( $aGoodsLst as $goodsid = $aGoods ) { $aGoods [ 'price' ]= $aGoods [ 'buy' ]==0?0:( $aGoods [ 'buymoney' ]/ $aGoods [ 'buy' ]); $aGoods [ 'stock' ]= $aGoods [ 'buy' ]- $aGoods [ 'sell' ]+ $aGoods [ 'return' ]; $aGoods [ 'stockmon
mb_convert_encoding這個(gè)函數(shù)是用來轉(zhuǎn)換編碼的。原來一直對(duì)程序編碼這一概念不理解,不過現(xiàn)在好像有點(diǎn)開竅了。
不過英文一般不會(huì)存在編碼問題,只有中文數(shù)據(jù)才會(huì)有這個(gè)問題。比如你用Zend Studio或Editplus寫程序時(shí),用的是gbk編碼,如果數(shù)據(jù)需要入數(shù)據(jù)庫(kù),而數(shù)據(jù)庫(kù)的編碼為utf8時(shí),這時(shí)就要把數(shù)據(jù)進(jìn)行編碼轉(zhuǎn)換,不然進(jìn)到數(shù)據(jù)庫(kù)就會(huì)變成亂碼。
mb_convert_encoding的用法見官方:
http://cn.php.net/manual/zh/function.mb-convert-encoding.php
做一個(gè)GBK To UTF-8
< ?php
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");
?>
再來個(gè)GB2312 To Big5
< ?php
header("content-Type: text/html; charset=big5");
echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");
?>
不過要使用上面的函數(shù)需要安裝但是需要先enable mbstring 擴(kuò)展庫(kù)。
PHP中的另外一個(gè)函數(shù)iconv也是用來轉(zhuǎn)換字符串編碼的,與上函數(shù)功能相似。
下面還有一些詳細(xì)的例子:
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 擴(kuò)展庫(kù),在 php.ini里將; extension=php_mbstring.dll 前面的 ; 去掉
mb_convert_encoding 可以指定多種輸入編碼,它會(huì)根據(jù)內(nèi)容自動(dòng)識(shí)別,但是執(zhí)行效率比iconv差太多;
string iconv ( string in_charset, string out_charset, string str )
注意:第二個(gè)參數(shù),除了可以指定要轉(zhuǎn)化到的編碼以外,還可以增加兩個(gè)后綴://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 會(huì)自動(dòng)將不能直接轉(zhuǎn)化的字符變成一個(gè)或多個(gè)近似的字符,//IGNORE 會(huì)忽略掉不能轉(zhuǎn)化的字符,而默認(rèn)效果是從第一個(gè)非法字符截?cái)唷?/p>
Returns the converted string or FALSE on failure.
使用:
發(fā)現(xiàn)iconv在轉(zhuǎn)換字符”—”到gb2312時(shí)會(huì)出錯(cuò),如果沒有ignore參數(shù),所有該字符后面的字符串都無法被保存。不管怎么樣,這個(gè)”—”都無法轉(zhuǎn)換成功,無法輸出。 另外mb_convert_encoding沒有這個(gè)bug.
一般情況下用 iconv,只有當(dāng)遇到無法確定原編碼是何種編碼,或者iconv轉(zhuǎn)化后無法正常顯示時(shí)才用mb_convert_encoding 函數(shù).
from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. If it is not specified, the internal encoding will be used.
/* 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”);
/* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */
$str = mb_convert_encoding($str, “EUC-JP”, “auto”);
例子:
$content = iconv(”GBK”, “UTF-8//IGNORE″, $content);
$content = mb_convert_encoding($content, “UTF-8″, “GBK”);
來源:http://www.cnblogs.com/zhwl/archive/2012/08/07/2626896.html
分享:php會(huì)話機(jī)制總結(jié)1 什么是會(huì)話 web中的會(huì)話實(shí)際上是指瀏覽器和web服務(wù)器之間的會(huì)話,形象一點(diǎn):就好比兩個(gè)人進(jìn)行一段對(duì)話。 2 php會(huì)話機(jī)制工作原理 當(dāng)瀏覽器向服務(wù)器發(fā)送URL請(qǐng)求,服務(wù)器會(huì)生成一個(gè)會(huì)話ID,并將瀏覽器端的一些信息保存在服務(wù)器端,然后將會(huì)話ID送到瀏覽器端保存到cookie
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php中mb_convert_encoding 和 iconv 的區(qū)別
。