1

I have a text file (similar to CSV concept) to parse and load into different columns.

I receive it from an external application I can't modify. It uses ";" as field separator but unfortunatly we can have the same char also inside some content.

Here a little sample:

Code;Name;Address;E_mail;Contact name
000001;FUTURAMA SNC;VIA BARBAPAPA, 1;info@gmail.com;matteo futuro;
000006;FERRANTIBUS SRL;VIA TOPOLINO, 1;amministrazione@gmail.com;nicola ferri;
000008;MORMORO SPA;VIA CICCETTI, 30;"cri@mormoro.it; rossi@mormoro.it";panebianco gianpietro;

we use this code to parse the file

    $file = fopen("C:\\wamp\\www\\testcsv\\customers.csv","r");
$result ="";
$i=0;

while(! feof($file))
{
    $result[$i++]=  fgets($file);
}

for($j=1;$j<count($result);$j++){
    $tempData = preg_split("/[;]/",$result[$j]);
    print_r( $tempData );
}

as you can see, in the last line of the sample file, we have ";" char inside email field.... so it is read as another column separator and in the third record the email field is splitted as 2 column, with the result I have an additional column.

Is there any way, using regular expression to skip ; char if it is inside the "" chars?

Thanks in advance for the help

agodoo
  • 409
  • 6
  • 21

1 Answers1

1

You should not use regexpr to parse CSV files.

Use native PHP function http://php.net/manual/en/function.fgetcsv.php

This will solve your problem.

Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46