使用fgetcsv读取CSV文件时出现UTF-8问题 [英] UTF-8 problems while reading CSV file with fgetcsv

查看:124
本文介绍了使用fgetcsv读取CSV文件时出现UTF-8问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试读取CSV并回应内容。但内容显示字符错误。



MäxMüstermänn - >MäxMüstermänn



CSV文件的编码是UTF-8



这是CSV文件的内容:



Mäx;Müstermänn



我的PHP脚本

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 dtd> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< / head>
< body>
<?php
$ handle = fopen(specialchars.csv,r);
echo'< table border =1>< tr>< td>名字< / td>< td>姓氏< / td>< / tr>< tr& ;
while($ data = fgetcsv($ handle,1000,;)){
$ num = count($ data);
for($ c = 0; $ c <$ num; $ c ++){
//输出数据
echo< td> $ data [$ c]< / td> ;;
}
echo< / tr>< tr>;
}
?>
< / body>
< / html>



我尝试使用 setlocale(LC_ALL,'de_DE.utf8'); 这里没有成功。



编辑:



b
$ b

一个 echo mb_detect_encoding($ data [$ c],'UTF-8'); 给我UTF-8 UTF-



echo file_get_contents(specialchars.csv); 给我 ;Müstermänn



 code> print_r(str_getcsv(reset(explode(\\\
,file_get_contents(specialchars.csv))),';'))

给我



Array([0] =>Mäx[1 ] =>M¼stermänn)



这是什么意思?

解决方案

现在我得到它的工作(删除命令后)。我认为问题是php文件的编码是在ISO-8859-1。我把它设置为UTF-8没有BOM。



此外,我使用了 SET NAMES'utf8'为数据库。现在它在数据库中也是正确的。


I try to read a CSV and echo the content. But the content displays the characters wrong.

Mäx Müstermänn -> Mäx Müstermänn

Encoding of the CSV file is UTF-8 without BOM (checked with Notepad++).

This is the content of the CSV file:

"Mäx";"Müstermänn"

My PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$handle = fopen ("specialchars.csv","r");
echo '<table border="1"><tr><td>First name</td><td>Last name</td></tr><tr>';
while ($data = fgetcsv ($handle, 1000, ";")) {
        $num = count ($data);
        for ($c=0; $c < $num; $c++) {
            // output data
            echo "<td>$data[$c]</td>";
        }
        echo "</tr><tr>";
}
?>
</body>
</html>

I tried to use setlocale(LC_ALL, 'de_DE.utf8'); as suggested here without success. The content is still wrong displayed.

What I'm missing?

Edit:

An echo mb_detect_encoding($data[$c],'UTF-8'); gives me UTF-8 UTF-8.

echo file_get_contents("specialchars.csv"); gives me "Mäx";"Müstermänn".

And

print_r(str_getcsv(reset(explode("\n", file_get_contents("specialchars.csv"))), ';'))

gives me

Array ( [0] => Mäx [1] => Müstermänn )

What does it mean?

解决方案

Now I got it working (after removing the header command). I think the problem was that the encoding of the php file was in ISO-8859-1. I set it to UTF-8 without BOM. I thought I already have done that, but perhaps I made an additional undo.

Furthermore, I used SET NAMES 'utf8' for the database. Now it is also correct in the database.

这篇关于使用fgetcsv读取CSV文件时出现UTF-8问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆