php fgetcsv 详解,PHP fgetcsv()分隔符(PHP fgetcsv() delimiter)

  • Post author:
  • Post category:php

PHP fgetcsv()分隔符(PHP fgetcsv() delimiter)

关于fgetcsv()文档, fgetcsv()函数中有一些参数:句柄,长度,分隔符,外壳和转义。

现在,我有两个问题:

外壳做了什么?

我有一个每行5列的csv文件。 想象一下这样的事情: 1,2,3,4,5

所以索引从0到4。但是每当我想从索引4获取日期,一个空值返回。 除非我在它之后加一个逗号(通过填充一个额外的列后面,使内容如下: 1,2,3,4,5,6 )。 我该如何解决这个问题? 似乎有一些问题,因为在csv文件的每一行中的最后一项之后缺少逗号!

Regarding to fgetcsv() documentation, there are some parameteres inside fgetcsv() function: Handle, length, delimiter, enclosure, and escape.

Now, I have 2 questions:

What does enclosure do exactly?

I have a csv file with 5 columns per line. Imagine it something like this: 1,2,3,4,5

So indexes are from 0 to 4. But whenever I want to get date from index 4, an empty value returns. Unless I put a comma after it (by filling an extra column after it that makes the contents like this: 1,2,3,4,5,6 ). How can I solve this issue ? It seems that there is some problem because of missing comma after the last item in each row of csv file!

原文:https://stackoverflow.com/questions/12075336

更新时间:2019-09-09 14:43

最满意答案

1. enclosure参数是封装特定字段或“索引”数据的字符。 默认情况下,该参数是” ,这意味着您可以”将字符串包含在”字符中。

例:

1,2,”this is three”,4

2.根据评论,您正在调用fgetcsv($handle, 10000, ‘,’) 。 也许,您正在阅读的行可能超过10000个字符。 尝试改变长度为0 ,这将是“没有限制”,看看是否有帮助。 另一种解决方法是尝试用双引号将列值包装起来。

1. The enclosure parameter is the character the encapsulates the data for a specific field, or “index”. By default, the parameter is a “, which means that you can “enclose” strings in ” characters.

Example:

1,2,”this is three”,4

2. Per a comment, you’re calling fgetcsv($handle, 10000, ‘,’). It’s possible, maybe, that the line(s) you’re reading are longer than 10000 characters. Try changing the length to 0, which will be “no limit” and see if that helps. Another solution would be to try wrapping the column’s value in double-quotes.

2012-08-22

相关问答

第二个参数是长度,所以你的fgetcsv应该是 fgetcsv($handle, 0, ‘;’);

导致 4 champs à la ligne 1:

First Name

Last Name

Email

Age

4 champs à la ligne 2:

Julie

Brown

julie@example.com

52

4 champs à la ligne 3:

Dan

Wong

dan@example.com

19

4 champs à la ligne 4:

Tim

H

1. enclosure参数是封装特定字段或“索引”数据的字符。 默认情况下,该参数是” ,这意味着您可以”将字符串包含在”字符中。 例: 1,2,”this is three”,4

2.根据评论,您正在调用fgetcsv($handle, 10000, ‘,’) 。 也许,您正在阅读的行可能超过10000个字符。 尝试改变长度为0 ,这将是“没有限制”,看看是否有帮助。 另一种解决方法是尝试用双引号将列值包装起来。 1. The enclosure parameter is the chara

$handle = @fopen($filename, “r”);

if ($handle) {

while (($buffer = fgets($handle, 4096)) !== false) {

$buffer = preg_replace(‘/(\\\”,)/’,’\\ “,’,$buffer);

$buffer = preg_replace(‘/(\\\”(“?),)/’,’ ‘,$buffer);

$data[] = str_g

在for循环中使用我的rawdata上的iconv()解决了我的问题: $data[$i][] = htmlentities(iconv(“cp1252”, “utf-8”, trim($rawdata[$i])), ENT_IGNORE, “UTF-8”);

谢谢PHP聊天中的@ Leigh,Wrikken和DaveRando;) Using iconv() on my rawdata in the for loop solved my issue: $data[$i][] = htmlent

使用相同的比较运算符 。 Use Identical Comparision Operator.

用可用性而不是代码解决这个问题。 让用户选择分隔符。 但是,由于他们可能不知道哪个制表符分隔,CSV等等,只是向他们展示预览。 他们可以从选项中选择,直到输出看起来正确和表格。 然后根据选择的格式解析它。 Solve this problem with usability instead of code. Have the user pick the delimiter. However, since they may not know what tab delimited, CSV, et al

如果填充的行数没有修复,您可以使用类似的方法: if (empty($line)) { break; }

您的评论给了我一个更好的解决方案的想法: 这里http://php.net/manual/en/function.fgetcsv.php出现这个“CSV文件中的空白行将作为包含单个空字段的数组返回”,所以这可能会有所帮助: while(($csv_line = fgetcsv($file,1024)) && $csv_line[0]) { 将只读取,直到找到一个空行。 If the numb

在我的’PHP DIGEST-MD5’的谷歌搜索中,我遇到了另一个项目的补丁,该项目使用以下行处理相同的格式字符串: preg_match_all(‘/(\w+)=(?:”([^”]*)|([^,]*))/’, $challenge, $matches);

这给了我: array (

0 =>

array (

0 => ‘realm=”smtp.domain.net’,

1 => ‘nonce=”AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn

fgetscv()将每行作为数组返回。 所以你的问题是这样的: $ data是一行,已经是一个数组。 while()循环中的每次迭代都将返回一行。 没有必要爆炸。 $allRowsAsArray = array();

if (!$fp=fopen($file,”r”)) echo “The file could not be opened.
“;

while (( $data = fgetcsv ( $fp , 1000 , ‘,’)) !== FALSE )

{

$allR

好旧的PHP,如果有一些半生不熟的标准功能可供人们忘记如何手动操作: $lines = explode(‘~’, file_get_contents($pathToMyFile));

foreach($lines as $line)

{

$values = explode(‘^’, $line);

}

根据转义策略(如果^或~出现在值中),您必须稍微调整一下。 Good old PHP, where people forget how to do stuff manually if ther