PHP fgetcsv () where the source file does not have a shell character?

I have an input CSV where the "columns" are not enclosed in anything.

File Contents ($ input = fopen ( filename );):

     1,2,3,4,5,6,7
     a, b, c, d, e, f, g
     9,8,7,6,5,4,3
     z, y, x, w, v, u, t

I am having problems working with fgetcsv () because there are no restrictions around the values.

while($row = fgetcsv($input)) { print_r($row); } 

Results in:

  1111

I tried some basic things that I could think of from my head:

 fgetcsc($file, 0, ',', '\'); fgetcsc($file, 0, ',', ''); 

Any ideas for work? Manually editing my input file is not really an option, as it is several million lines.

+4
source share
3 answers

It is not clear what the problem is. fgetcsv and str_getcsv also work with columns without quotes:

 print_r(str_getcsv('1,2,3,4,5,6,7')); 

Return:

 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) 

Quotation marks are optional and are for CSV only, which may have column delimiters within the range of values:

  column1, "Column 2 with , comma inside", colum3 
+4
source

You do not need to use these last two parameters at all, they are optional. Just use:

 fgetcsv($file, 1000); 

This automatically assumes the delimiter is a comma. Also, the length should not be 0, but a positive number of characters to be read.

Alternatively: you do not need to rely on fgetcsv , you can just use explode :

 $values = explode(",", $row); 
+1
source

a better way to do this is to change the cabinet value to something that is rarely used (or not used at all).

the best variation of this, I found, would be the symbol chr (0).

 fgetcsv($handle, null, "\t", chr(0)) 

works great for me and shouldn't break unless you have a very extreme case of CSV data.

0
source

Source: https://habr.com/ru/post/1381969/


All Articles