= 4.0.5, PHP 5, PHP 7, PHP 8)iconv — 字符串按要求的字符編碼來轉(zhuǎn)換說明iconv(string $in_charset, string $out_charset, string $str): string將字符串 str 從 in_cha">

iconv

(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)

iconv字符串按要求的字符編碼來轉(zhuǎn)換

說明

iconv(string $in_charset, string $out_charset, string $str): string

將字符串 strin_charset 轉(zhuǎn)換編碼到 out_charset。

參數(shù)

in_charset

輸入的字符集。

out_charset

輸出的字符集。

如果你在 out_charset 后添加了字符串 //TRANSLIT,將啟用轉(zhuǎn)寫(transliteration)功能。這個意思是,當(dāng)一個字符不能被目標(biāo)字符集所表示時,它可以通過一個或多個形似的字符來近似表達。 如果你添加了字符串 //IGNORE,不能以目標(biāo)字符集表達的字符將被默默丟棄。 否則,會導(dǎo)致一個 E_NOTICE并返回 false。

警告

//TRANSLIT 運行細節(jié)高度依賴于系統(tǒng)的 iconv() 實現(xiàn)(參見 ICONV_IMPL)。 據(jù)悉,某些系統(tǒng)上的實現(xiàn)會直接忽略 //TRANSLIT,所以轉(zhuǎn)換也有可能失敗,out_charset 會是不合格的。

str

要轉(zhuǎn)換的字符串。

返回值

返回轉(zhuǎn)換后的字符串, 或者在失敗時返回 false。

更新日志

版本 說明
5.4.0 這個版本起,字符非法時候會返回 false,除非在輸出字符里指定了 //IGNORE 。 在之前版本,它會返回一部分字符串。

范例

示例 #1 iconv() 例子

<?php
$text 
"This is the Euro symbol '€'.";

echo 
'Original : '$textPHP_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