Iconv 从 UTF-8 转换为 Windows-1250 不起作用
Iconv convert from UTF-8 to Windows-1250 doesn't work
我一直对 iconv 有疑问。现在我必须将字符串转换为 Windows-1250,这似乎不起作用:
$string = "ľaľa ho papľuha, ogrcal mi krpce!";
echo $string . ' ( ' . mb_detect_encoding($string) . ' ) <br>';
$string_encoded = iconv( mb_detect_encoding( $string ), 'Windows-1250//TRANSLIT', $string );
echo $string_encoded . ' ( ' . mb_detect_encoding($string_encoded) . ' ) <br>';
$string_encoded = mb_convert_encoding( $string, 'Windows-1250' );
echo $string_encoded . ' ( ' . mb_detect_encoding($string_encoded) . ' ) <br>';
上面的三个回显输出正是这样的:
ľaľa ho papľuha, ogrcal mi krpce! ( UTF-8 )
�a�a ho pap�uha, ogrcal mi krpce! ( )
mb_convert_encoding() Unknown encoding "Windows-1250" ( ASCII )
因为我总是看到这个菱形问号,所以我想知道这个 PHP 功能是否有效。如何将 UTF-8 转换为 Windows-1250?
- 文件以 UTF-8 格式保存在记事本+中
- 我也试过
header('Content-Type: text/html; charset=windows-1250');
和 setLocale()
� 字符表示您的文本被解释为 UTF-8,但此时遇到了无效的字节序列。意思是,您没有提供 UTF-8 服务,但客户端正在将其读取为 UTF-8。这意味着 iconv
工作得很好,无论谁正在阅读结果,都没有得到应该将其解释为 Windows-1250.
的消息
见What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App。
旧 post 但您可以将 UTF-8 转换为 Windows-1252 并且您将获得相同的效果:
$str = "ľaľa ho papľuha, ogrcal mi krpce!"
$str = mb_convert_encoding( $str, "Windows-1252", "UTF-8" );
但如果您确实需要 Windows-1250,您可以使用 THIS SOLUTION 并适应您的需要。
我遇到过类似的问题。读取 CSV 文件时,单词“Česká republika”被读为“Èeská republika”。
这为我解决了:
iconv( "Windows-1250", "UTF-8", ($string));
正确答案是iconv( "UTF-8", "Windows-1250", $string );
我一直对 iconv 有疑问。现在我必须将字符串转换为 Windows-1250,这似乎不起作用:
$string = "ľaľa ho papľuha, ogrcal mi krpce!";
echo $string . ' ( ' . mb_detect_encoding($string) . ' ) <br>';
$string_encoded = iconv( mb_detect_encoding( $string ), 'Windows-1250//TRANSLIT', $string );
echo $string_encoded . ' ( ' . mb_detect_encoding($string_encoded) . ' ) <br>';
$string_encoded = mb_convert_encoding( $string, 'Windows-1250' );
echo $string_encoded . ' ( ' . mb_detect_encoding($string_encoded) . ' ) <br>';
上面的三个回显输出正是这样的:
ľaľa ho papľuha, ogrcal mi krpce! ( UTF-8 )
�a�a ho pap�uha, ogrcal mi krpce! ( )
mb_convert_encoding() Unknown encoding "Windows-1250" ( ASCII )
因为我总是看到这个菱形问号,所以我想知道这个 PHP 功能是否有效。如何将 UTF-8 转换为 Windows-1250?
- 文件以 UTF-8 格式保存在记事本+中
- 我也试过
header('Content-Type: text/html; charset=windows-1250');
和setLocale()
� 字符表示您的文本被解释为 UTF-8,但此时遇到了无效的字节序列。意思是,您没有提供 UTF-8 服务,但客户端正在将其读取为 UTF-8。这意味着 iconv
工作得很好,无论谁正在阅读结果,都没有得到应该将其解释为 Windows-1250.
见What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App。
旧 post 但您可以将 UTF-8 转换为 Windows-1252 并且您将获得相同的效果:
$str = "ľaľa ho papľuha, ogrcal mi krpce!"
$str = mb_convert_encoding( $str, "Windows-1252", "UTF-8" );
但如果您确实需要 Windows-1250,您可以使用 THIS SOLUTION 并适应您的需要。
我遇到过类似的问题。读取 CSV 文件时,单词“Česká republika”被读为“Èeská republika”。
这为我解决了:
iconv( "Windows-1250", "UTF-8", ($string));
正确答案是iconv( "UTF-8", "Windows-1250", $string );