【问题标题】:Encoding issue with PHP while writing in a .csv file写入 .csv 文件时 PHP 的编码问题
【发布时间】:2019-08-26 10:53:08
【问题描述】:

我正在使用一个 php 数组,其中包含一些从之前的抓取过程中解析的值(使用 Simple HTML DOM Parser)。我可以正常print/echo这个数组的值,其中包含特殊字符é,à,èBUT,问题如下:

当我使用fwrite 将值保存在 .csv 文件中时,某些字符未成功保存。例如,SzékesfehérvárHTML 的我的php 视图中很好地显示,但在我使用上面的php 脚本生成的.csv 文件中另存为Székesfehérvár

我已经在 php 脚本中设置了一些东西:

  • 我正在抓取的页面似乎是 utf-8 编码的
  • 我的 PHP 脚本也在标头中声明为 utf-8
  • 我在代码的不同位置尝试了很多iconvmb_encode 方法
  • 注意,当我使用 json_encode 制作我的 php 数组的 JS console.log 时,字符也会损坏,可能与我正在抓取的页面的原始编码有关?

这是脚本的一部分,它是在.csv文件中写入值的部分

<?php 

$data = array(
            array("item1", "item2"), 
            array("item1", "item2"),
            array("item1", "item2"),
            array("item1", "item2")
            // ...
);

//filename
$filename = 'myFileName.csv';

foreach($data as $line) {
    $string_txt = ""; //declares the content of the .csv as a string
    foreach($line as $item) {
        //writes a new line of the .csv
        $line_txt = "";
        //each line of the .csv equals to the values of the php subarray, tab separated
        $line_txt .= $item . "\t";
    }

    //PHP endline constant, indicates the next line of the .csv
    $line_txt .= PHP_EOL;
    
    //add the line to the string which is the global content of the .csv
    $line_txt .= $string_txt;
}

//writing the string in a .csv file 
$file = fopen($filename, 'w+');
fwrite($file, $string_txt);
fclose($file);

我目前卡住了,因为我无法正确保存带有重音字符的值。

【问题讨论】:

  • “我正在抓取的页面似乎是 utf-8 编码的” - 更确切地说,您正在抓取的页面实际上使用这些数字实体来表示这些字符已经。您可能只是没有注意到,因为您在浏览器将它们解释为 HTML 之后查看了调试输出。 html_entity_decode 应该有帮助。
  • @misorude,感谢您的帮助。我不太明白你的评论,让我补充一点:当我对我的$data 数组执行print_r 时,所有字符都是可用的,但问题是当我尝试用这个数组做其他事情时,比如作为 JS 的 json_encode,或写入 .csv。你明白我的意思吗 ?谢谢
  • 做一个print_r("Sz&amp;#233;kes"); - 注意到什么了吗?
  • 是的,print_r 返回Székes。跟着你,我使用htmlentities 来获取值的 original 数字实体,但我现在的问题是:我如何将值 storeSzékes 例如,而不是Sz&amp;#233;kes?谢谢@misorude
  • “是的,print_r 返回Székes - 那么你现在明白我最初的评论了吗? “我如何将值存储为Székes,而不是Sz&amp;#233;kes?” - 通过将你拥有的值变成你想要的值 - 你目前 Sz&amp;#233;kes。不,我没有说要使用htmlentities

标签: php web-scraping utf-8 character-encoding


【解决方案1】:

将此行放入您的代码中

header('Content-Type: text/html; charset=UTF-8');

希望对您有所帮助!

【讨论】:

  • 谢谢,不过已经在脚本里了,不过我简化了帖子的代码部分。 (文中也提到:“我的 PHP 脚本在 header 中也声明为 utf-8”)。谢谢
  • 当您要打开下载的 CSV 文件时,请您选择编码类型:UTF-8 检查一次。
【解决方案2】:

试试看


$file = fopen('myFileName.csv','w');
$data= array_map("utf8_decode", $data);
fputcsv($file,$data);

【讨论】:

    【解决方案3】:

    Excel 在显示 utf8 编码的 csv 文件时出现问题。我以前看过这个。但是你可以试试 utf8 BOM。我试过了,对我有用。这只是在 utf8 字符串的开头添加这些字节:

    $line_txt .= chr(239) . chr(187) . chr(191) . $item . "\t";
    

    更多信息: Encoding a string as UTF-8 with BOM in PHP

    或者,您可以使用 Excel 中的文件导入功能并确保文件来源为 65001 : Unicode(UTF8)。它应该正确显示您的文本,您需要将其保存为 Excel 文件以保留格式。

    【讨论】:

    • 非常感谢,我终于通过html_entity_decode找到了答案。
    【解决方案4】:

    解决方案(由@misorude 提供):

    抓取网页中的 HTML 内容时,调试中显示的内容与脚本中真正抓取的内容之间存在差异。我必须使用html_entity_decode 让 PHP 解释我 抓取 的 HTML 代码的 true 值,而不是浏览器的解释。

    要在将值存储在某处之前验证是否可以很好地检索值,您可以尝试在 JS 中使用 console.log 来查看值是否正确驱动:

    PHP

    //decoding numeric HTML entities who represents "Sóstói Stadion"
    $b = html_entity_decode("S&#243;st&#243;i Stadion"); 
    

    Javascript(测试):

    <script>
    var b = <?php echo json_encode($b) ;?>;
    
    //print "Sóstói Stadion" correctly
    console.log(b); 
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多