PHP fgetcsv()中未读取CSV文件中的某些字符, [英] Some characters in CSV file are not read during PHP fgetcsv()

查看:120
本文介绍了PHP fgetcsv()中未读取CSV文件中的某些字符,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php读取CSV文件。许多行有一个复选标记,这是真正的平方根符号:√和php代码只是跳过这个字符每次遇到。

I am reading a CSV file with php. Many of the rows have a "check mark" which is really the square root symbol: √ and the php code is just skipping over this character every time it is encountered.

这是我的代码(打印到浏览器窗口的CSV风格格式,所以我可以检查线断裂在正确的地方:

Here is my code (printing to the browser window in "CSV style" format so I can check that the lines break at the right place:

$file = fopen($uploadfile, 'r');
while (($line = fgetcsv($file)) !== FALSE) {
   foreach ($line as $key => $value) {
    if ($value) {
       echo $value.",";
    }
   }
   echo "<br />";
}
fclose($file);

,我只是在Excel中手动找到并用1替换复选标记。显然,我想要一个更有效的解决方案:)感谢您的帮助!

As an interim solution, I am just finding and replacing the checkmarks with 1's manually, in Excel. Obviously I'd like a more efficient solution :) Thanks for the help!

推荐答案

fgetcsv()仅适用于标准ASCII字符;所以它可能是正确的,跳过你的平方根符号。但是,不是手动替换复选标记,您可以将该文件读入字符串,对这些字符执行str_replace(),然后使用fgetcsv()解析它。您可以将字符串转换为文件指针(对于fgetcsv):

fgetcsv() only works on standard ASCII characters; so it's probably "correct" in skipping your square root symbols. However, rather than replacing the checkmarks manually, you could read the file into a string, do a str_replace() on those characters, and then parse it using fgetcsv(). You can turn a string into a file pointer (for fgetcsv) thusly:

$fp = fopen('php://memory', 'rw');
fwrite($fp, (string)$string);
rewind($fp);
while (($line = fgetcsv($fp)) !== FALSE)
...

这篇关于PHP fgetcsv()中未读取CSV文件中的某些字符,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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