douba3943 2013-08-30 11:48
浏览 75

编码:将excel电子表格中的特殊字符导出为php数组

I am trying to get the information from an excel spreadsheet into a PHP array. The problem being that the entries in the spreadsheet include special accented characters such as á and ž etc.

The first thing I did is to export the spreadsheet as a .txt file. I made sure to click on tools --> web options, in order to set the character encoding to UTF-8.

When I opened the .txt file with notepad or notepad++ many (but not all) of the accented characters were replaced with '?'.

for example: Zlatan Ibrahimović ----> Zlatan Ibrahimovi?

However, some of the characters remained unaffected: Carlos Tévez

Furthermore, when I attempted to import the .txt file into a PHP array, the remaining characters turned into question marks aswell!

The following code, is intended to print the first 4 names in the list:

$file = fopen("players/player_names.txt", "r") or exit("Unable to open file!");

$line_array = array();
$x = 0;
while(!feof($file))
{
array_push($line_array, fgets($file));
}
fclose($file);

$name_array = array();

for ($i =1; $i < 4; $i++ )
{
echo $line_array[$i];
echo "<br/>";   
}

However, the following is displayed:

Sergio Ag�ero Zlatan Ibrahimovi? Carlos T�vez Luis Su�rez

It appears that the normal question marks '?' are being produced in the transfer from excel to .txt, and the black background question marks of the remaining accented characters '�' are being produced when importing the .txt file to PHP.

I have made sure the character encoding of the PHP file is set to UTF-8 with the following:

header ('Content-type: text/html; charset=utf-8');

Where am I going wrong!?

Thanks for your help in advance :).

  • 写回答

1条回答 默认 最新

  • dqm74406 2013-08-30 12:01
    关注

    I have tested your code: header is not important, make sure you have in your php.ini

    default_charset = "UTF-8"
    

    Moreover, you can forget about the Content-Type header leaving this task to the webserver. Nginx, for example, has the charset UTF-8; directive.

    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?