我有以下地址:Praha 5, Staré Město,
在将其写入PDF文件(使用domPDF库)之前,我需要对此字符串使用utf8_decode()函数。
然而,上述地址行的php utf8解码函数似乎不正确(或者说不完整)。
以下是代码:
<?php echo utf8_decode('Praha 5, Staré Město,'); ?>
产生如下结果:
Praha 5,Staré M?sto,
有任何想法为什么ě无法解码?
我有以下地址:Praha 5, Staré Město,
在将其写入PDF文件(使用domPDF库)之前,我需要对此字符串使用utf8_decode()函数。
然而,上述地址行的php utf8解码函数似乎不正确(或者说不完整)。
以下是代码:
<?php echo utf8_decode('Praha 5, Staré Město,'); ?>
产生如下结果:
Praha 5,Staré M?sto,
有任何想法为什么ě无法解码?
utf8_decode
函数将字符串从UTF-8编码转换为ISO-8859-1,也就是“Latin-1”格式。
然而,“Latin-1”无法表示字母“ě”,这很简单明了。
“Decode”的命名存在误导,实际上它的功能与iconv('UTF-8', 'ISO-8859-1', $string)
相同。
utf8_decode
将字符串从 UTF-8 编码转换为 ISO-8859-1。这让我节省了几个小时的时间!如果您在我们的办公室,我很乐意请您喝一杯 :) - whizzkidfunction charset_decode_utf_8 ($string) {
/* Only do the slow convert if there are 8-bit characters */
/* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
if (! ereg("[\200-\237]", $string) and ! ereg("[\241-\377]", $string))
return $string;
// decode three byte unicode characters
$string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
"'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",
$string);
// decode two byte unicode characters
$string = preg_replace("/([\300-\337])([\200-\277])/e",
"'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
$string);
return $string;
}
UTF-8
编码,即使不使用utf8_decode
也可以。如果从数据库中获取这些数据'Praha 5, Staré Město,'
,最好将其字符集更改为UTF-8
。你不需要那个 (@Rajeev: 这个字符串会自动被检测为 utf-8 编码):
echo mb_detect_encoding('Praha 5, Staré Město,');
将始终返回UTF-8。