csv - PHP fgetcsv() and str_getcsv() Not Parsing With Double Enclosure Next to Delimiter -


i have parse csv file php. csv file provided client , not have control on format. comma delimited , uses double quotes text qualifiers. however, if field, such address field, has comma in it, client's system surrounds field in additional set of double quotes. example:

"9999x111","x1110000110105","john doe",""123 central park avenue, #108"","new york ny 10006 ","","","m","0","1","370.20" 

as can see, 4th field (3rd index) has set of double quotation marks around entire field. if send string through fgetcsv() or str_getcsv(), field not handled correctly.

unwanted result array: [0] => 9999x111 [1] => x1110000110105 [2] => john doe [3] => 555 central park avenue [4] =>  #108"" [5] => new york ny 10006 

if remove set of double quotation marks manually, line processed correctly using either function; however, wouldn't able in production environment.

preferred result array: [0] => 9999x111 [1] => x1110000110105 [2] => john doe [3] => 555 central park avenue, #108 [4] => new york ny 10006 

here current code using:

$filechg = fopen($fileloc['inputfile'], "r"); $cnt = 0; while(!feof($filechg)) {     $chg[$cnt] = fgetcsv($filechg,0,",","\"");     if($chg[$cnt]=="") { //unset blank arrays         unset($chg[$cnt]);     }     $cnt++; } 

i have tried variety of suggestions on stack overflow, php manual , more , can't seem working. if manually escape inner set of double quotation marks backslash, still incorrect result array. no matter how play either function script mess , try split field @ comma following "avenue" , ignores remaining "".

i feel if comment on php site may explaining what's happening, new coder unable visualize what's going on.

http://www.php.net/manual/en/function.fgetcsv.php#58124

i have tried following suggestions (out of many) no avail.

fgetcsv not splition data properly str_getcsv not parsing data correctly

this method have worked; requires number of fields on each line same.

reading csv file unescaped enclosures

i using php 5.3.27 on mac os x 10.8.

thank in advance taking look.

i able solve problem expanding on comments left daniel , cosades. instead of using fgetcsv() process line immediately, use fgets() store line in variable ($line). then, used stripos() find find location of every occurrence of repeating double quote (""). then, identifying locations need edited determining if character before or after not comma (,). below new code.

$filechg = fopen($fileloc['charge'], "r"); $cnt = 0;  while(($line=fgets($filechg))!==false){     $pos = 0;     while($pos=stripos($line,"\"\"",$pos)){         $chra = substr($line,$pos-1,1);         $chrb = substr($line,$pos+2,1);          if($chra!=","){             $line   = substr_replace($line,"",$pos+1,1);         }          if($chrb!=","){             $line   = substr_replace($line,"",$pos+1,1);         }             $pos = $pos + strlen(",\"\"");     }      if($line!=""){         $chg[$cnt] = str_getcsv($line,",","\"");     }      if($chg[$cnt]==""){         unset($chg[$cnt]);     }          $cnt++; } 

thanks pointing me in right direction!


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -