2

I have a PHP file that is pulling information in from a CSV file and is successfully turning it into a table that only shows specific rows and columns.

However, the CSV file has entries in it that have a comma and space in them, wrapped in quotation marks. My PHP file is splitting these entries into two, because of the comma, and is displaying the quote marks instead of using the quote marks to indicate that this is a field that includes a comma and space.

For example, the CSV file looks like this:

AA,BB,"Surname, Initial",CC,DD,EE

"Surname, and Initial" should be in the same field in the resulting table, but instead two fields are being created.

I really would like to solve this in the PHP code, and not by tinkering with the CSV file itself. The code is here:

echo "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>
      <link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">";
$file = 'CSV/test.csv';   
$lines = file($file);
if (file_exists($file)){
  //echo "$file exists<br/>"; 

echo '<table>';                     //create the html table
$x=2;       //define the first line to display
$y=20;       //define the last line to display
$line_counter=0;
foreach ($lines as $line) {         //loop to fill your table with csv data
  if($x<=$line_counter && $line_counter<=$y)
  {
    $expl = explode(",", $line);      //explode each line on "," to create the rows
    echo "<tr>";                      //create each line of your table
    $c=0;
    while ($c<=47) {                  //loop to extract columns 2 to 47
      if(($c % 2) == 1){              //odd  rows
        echo "<td class=\"odd\">".$expl[$c]."</td>";
      }
      elseif(($c == 0)){
        echo "<td class=\"even\">".$expl[$c]."</td>";
      }
      else{
        echo "<td class=\"even\">".$expl[$c]."</td>";    //even rows
      }
      $c++;
    }
    echo "</tr>";
    $x++;
  }
  $line_counter++;
}
echo "</table>";
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2653322
  • 105
  • 2
  • 10
  • 1
    Why don't you use the built-in [fgetcsv()](http://www.php.net/manual/en/function.fgetcsv.php) function to handle the file reading.... it's written specifically for this purpose, and will handle the quotes for you, and your problem will magically disappear – Mark Baker Aug 16 '13 at 20:20

1 Answers1

1

The built in fgetcsv() function should handle any proper CSV format. It has parameters that let you change both the delimiter and the expected quote type. If the CSV isn't broken, this function should parse it fine. Try swapping it in for your own parser, tweak the params as necessary for your CSV format, and then loop over the resulting array instead.

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

verv
  • 686
  • 4
  • 7
  • I've had a look at fgetcsv(), and have made it work for displaying a table and omitting the quote marks, but I cannot work out how to combine that with the code above that displays certain rows and columns. Can you help? I'm very new to php and would appreciate a walk through or pointing to a tutorial that helps a beginner. – user2653322 Aug 20 '13 at 12:57