Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fgetcsv escape '\'

Tags:

php

A csv file contains a '\' character in one of its record for which i am not able to get the exact number of key value pair in the array.

I am using PHP fgetcsv function as follows-

 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
   ....
 }

I have also tried escaping the characters as follows.

 while (($data = fgetcsv($handle, 1000, ",",'"',"\\")) !== FALSE) {
    ....
 }

But it did not work.What might be the possible solution?

like image 482
Sitansu Avatar asked Nov 16 '11 10:11

Sitansu


People also ask

How to use fgetcsv in PHP?

PHP fgetcsv() Function$file = fopen("contacts. csv","r"); print_r(fgetcsv($file)); fclose($file);

How do you check csv file is empty or not in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.


1 Answers

I had the same problem, and solve it too easy. If your string enclosure charater is '"' ( double quotes ), and you just don't want to consider the backslash as a escape character, just inform the double quote twice, as the string enclusure and as a escape character.

    $data = fgetcsv($handle, 8192, ';' , '"' , '"'  );
like image 176
siga0984 Avatar answered Sep 18 '22 00:09

siga0984