physicalattraction
physicalattraction

Reputation: 6858

Make PHP getcsv more strict about double quotes

I have a file with a csv file, but for this question, I show my problem with a string. I want to validate that a certain csv file meets a number of requirements. One of them is that all fields shall be quoted using double quotes. Another one is that double quotes within a field shall be escaped by doubling the double quote character. This is shown in the code snippet below.

$a = 'Incorrect:ABC;"Incorrect:A"B"C";"Correct:A""B""C"';
$line = str_getcsv($a, ";", '"');
var_dump($line);

If I run this in Teh Playground, I get the following result.

array(3) {
  [0]=>
  string(13) "Incorrect:ABC"
  [1]=>
  string(15) "Incorrect:AB"C""
  [2]=>
  string(13) "Correct:A"B"C"
}
  1. Is there a way to make the reading of the first two fields failusing str_getcsv or fgetcsv?
  2. If not, is there a way to recognize later if one of these faulty conditions occurred?

Upvotes: 1

Views: 211

Answers (1)

Kuba Wyrostek
Kuba Wyrostek

Reputation: 6221

You could go with a very simple automata that checks each single line of a file:

<?php

  $a = 'Incorrect:ABC;"Incorrect:A"B"C";"Correct:A""B""C"';

  $state = 0;
  for ($i = 0; $i < strlen($a); $i++) {
    $c = $a[$i];
    if ($state == 0 && $c == "\"")
      $state = 1;
    else if ($state == 1 && $c == "\"")
      $state = 2;
    else if ($state == 2 && $c == ";")
      $state = 0;
    else if ($state == 2 && $c == "\"") // double "
      $state = 1;
    else if ($state == 1)
      $state = 1; // no-op; consume next character
    else {
      echo('Failed at character: ' . ($i + 1));
      exit();
    }
  }
  if ($state !== 2)
    echo('Line incomplete');
  else
    echo('Line OK');

?>

Upvotes: 1

Related Questions