iconv — Convert string to requested character encoding(PHP 4 >= 4.0.5, PHP 5) php
mb_convert_encoding — Convert character encoding(PHP 4 >= 4.0.6, PHP 5) iconv — 字符串按要求的字符編碼來轉換
mb_convert_encoding — 轉換字符的編碼 正則表達式
這兩個函數功能相似都是用來轉換字符串編碼的;
函數
參數:str——要編碼的str、to_encoding——str要轉換成編碼類型、from_encoding——在轉換前經過字符代碼名稱來指定。 它能夠是一個 array 也能夠是逗號分隔的枚舉列表。 若是沒有提供 from_encoding,則會使用內部(internal)編碼。 參見支持的編碼。 編碼
支持的字符編碼
當前 mbstring 模塊支持如下的字符編碼。這些字符編碼中的任意一個都能指定到 mbstring 函數中的 encoding 參數。
該 PHP 擴展支持的字符編碼有如下幾種:
UCS-4*
UCS-4BE
UCS-4LE*
UCS-2
UCS-2BE
UCS-2LE
UTF-32*
UTF-32BE*
UTF-32LE*
UTF-16*
UTF-16BE*
UTF-16LE*
UTF-7
UTF7-IMAP
UTF-8*
ASCII*
EUC-JP*
SJIS*
eucJP-win*
SJIS-win*
ISO-2022-JP
ISO-2022-JP-MS
CP932
CP51932
SJIS-mac** (別名: MacJapanese)
SJIS-Mobile#DOCOMO** (別名: SJIS-DOCOMO)
SJIS-Mobile#KDDI** (別名: SJIS-KDDI)
SJIS-Mobile#SOFTBANK** (別名: SJIS-SOFTBANK)
UTF-8-Mobile#DOCOMO** (別名: UTF-8-DOCOMO)
UTF-8-Mobile#KDDI-A**
UTF-8-Mobile#KDDI-B** (別名: UTF-8-KDDI)
UTF-8-Mobile#SOFTBANK** (別名: UTF-8-SOFTBANK)
ISO-2022-JP-MOBILE#KDDI** (別名: ISO-2022-JP-KDDI)
JIS
JIS-ms
CP50220
CP50220raw
CP50221
CP50222
ISO-8859-1*
ISO-8859-2*
ISO-8859-3*
ISO-8859-4*
ISO-8859-5*
ISO-8859-6*
ISO-8859-7*
ISO-8859-8*
ISO-8859-9*
ISO-8859-10*
ISO-8859-13*
ISO-8859-14*
ISO-8859-15*
byte2be
byte2le
byte4be
byte4le
BASE64
HTML-ENTITIES
7bit
8bit
EUC-CN*
CP936
GB18030**
HZ
EUC-TW*
CP950
BIG-5*
EUC-KR*
UHC (CP949)
ISO-2022-KR
Windows-1251 (CP1251)
Windows-1252 (CP1252)
CP866 (IBM866)
KOI8-R*
* 表示該編碼也能夠在正則表達式中使用。
** 表示該編碼自 PHP 5.4.0 始可用。
任何接受編碼名稱的 php.ini 條目一樣也可使用 "auto" 和 "pass" 的值。 接受編碼名的 mbstring 函數一樣也可使用值 "auto"。
若是設置了 "pass",將不會對字符的編碼進行轉化。
若是設置了 "auto",它將擴展成 NLS 中定義的每一個字符編碼列表。 好比,假設 NLS 設置爲 Japanese,值將會認爲是 "ASCII,JIS,UTF-8,EUC-JP,SJIS"。
NLS:國家語言支持(National Language Support) spa
<?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"); ?>
<?php $text = "This is the Euro symbol '€'."; echo 'Original : ', $text, PHP_EOL; echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL; echo 'IGNORE : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL; echo 'Plain : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL; ?> 輸出結果: Original : This is the Euro symbol '€'. TRANSLIT : This is the Euro symbol 'EUR'. IGNORE : This is the Euro symbol ''. Plain : Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7 This is the Euro symbol '