utf8_encode() 无法正确转换少数 non-English/Diacritic 个字符
utf8_encode() not able to convert properly few non-English/Diacritic characters
我的情况很奇怪。我的 csv 文件中的以下文本和文件在 notpade++ 上显示为 ANSI 编码。
Ÿ
下面是我的示例代码:
<?php
header('Content-Type: text/html; charset=UTF-8');
$handle = fopen("unicode.csv", "r");
while (($line = fgets($handle)) !== FALSE)
{
$cur_encoding = mb_detect_encoding($line) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($line,"UTF-8"))
{
echo "\r\n UTF-8".$line;
}
else
{
echo "\r\n encode UTF-8".utf8_encode($line);
}
}?>
我发现的代码有问题:
- 无法检测编码。
- 缺少两个字符。 (Œœ 和 Ÿ)
请帮我找出这两个字符丢失的原因。
另一个奇怪的行为是它在 Chrome 中显示字符,但在 FF 或 IE 中不显示
注意:如果我使用Notepad++将编码转换为UTF-8,我就能成功读取。所以请不要建议这个解决方案。
Get the csv file here
试试这个:
$encoding = mb_detect_encoding($line, array(
'UTF-8', 'ASCII'
));
var_dump(iconv($encoding, 'UTF-8', $line));exit;
该文件编码为代码页 1252 a.k.a。 MS-ANSI a.k.a。 WINDOWS-1252a.k.a。 Windows 拉丁文 1. 将其转换为 UTF-8:
echo iconv('CP1252', 'UTF-8', file_get_contents('unicode.csv'));
我的情况很奇怪。我的 csv 文件中的以下文本和文件在 notpade++ 上显示为 ANSI 编码。
Ÿ
下面是我的示例代码:
<?php
header('Content-Type: text/html; charset=UTF-8');
$handle = fopen("unicode.csv", "r");
while (($line = fgets($handle)) !== FALSE)
{
$cur_encoding = mb_detect_encoding($line) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($line,"UTF-8"))
{
echo "\r\n UTF-8".$line;
}
else
{
echo "\r\n encode UTF-8".utf8_encode($line);
}
}?>
我发现的代码有问题:
- 无法检测编码。
- 缺少两个字符。 (Œœ 和 Ÿ)
请帮我找出这两个字符丢失的原因。 另一个奇怪的行为是它在 Chrome 中显示字符,但在 FF 或 IE 中不显示 注意:如果我使用Notepad++将编码转换为UTF-8,我就能成功读取。所以请不要建议这个解决方案。 Get the csv file here
试试这个:
$encoding = mb_detect_encoding($line, array(
'UTF-8', 'ASCII'
));
var_dump(iconv($encoding, 'UTF-8', $line));exit;
该文件编码为代码页 1252 a.k.a。 MS-ANSI a.k.a。 WINDOWS-1252a.k.a。 Windows 拉丁文 1. 将其转换为 UTF-8:
echo iconv('CP1252', 'UTF-8', file_get_contents('unicode.csv'));