Posted By

Sverri on 08/17/10


Tagged

csv


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

convergine
mikael12


Simple CSV Reader


 / Published in: PHP
 

See code comments for more details.

  1. /*
  2.   Simple CSV Reader
  3.  
  4.   So, a few ground rules:
  5.   - Rows must end with a newline
  6.   - Rows cannot have newlines in them
  7.   - All values must be enclosed in double quotes.
  8.   - Double quotes are allowed inside values.
  9.  
  10.   If the file is not formatted according to these rules then
  11.   the world as we know it will cease to exist.
  12.  
  13.   Available public methods:
  14.   - log(): Returns an array log if something is out of order.
  15.  
  16.   Have a good day.
  17. */
  18.  
  19. class CSVreader
  20. {
  21. private $log;
  22.  
  23. public function __construct($filename = NULL)
  24. {
  25. if ($filename)
  26. {
  27. $this->open($filename);
  28. }
  29. }
  30.  
  31. private function open($filename)
  32. {
  33. if ( ! file_exists($filename)) die ("File $filename does not exist.");
  34.  
  35. if ( ! is_readable($filename)) die ("File $filename is not readable.");
  36.  
  37. $lines = file($filename, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
  38.  
  39. $fields = $this->line_values( array_shift($lines) );
  40.  
  41. $fieldCount = count($fields);
  42.  
  43. foreach($lines as $key => $values)
  44. {
  45. $values = $this->line_values($values);
  46.  
  47. $valueCount = count($values);
  48.  
  49. $line = $key + 2;
  50.  
  51. if ($valueCount < $fieldCount)
  52. {
  53. $missing = $fieldCount - $valueCount;
  54.  
  55. for ($x=0; $x<$missing; $x++) $values[] = '(n/a)';
  56.  
  57. $this->log_message($line, "$missing ".($missing==1?'value is':'values are')." missing");
  58. }
  59. if ($valueCount > $fieldCount)
  60. {
  61. $tooMany = $valueCount - $fieldCount;
  62.  
  63. $this->log_message($line, "There ".($tooMany==1?"is 1 value":"are $tooMany values")." too many");
  64. }
  65. $i = 0;
  66.  
  67. foreach ($values as $val)
  68. {
  69. if (empty($val))
  70. {
  71. $this->log_message($line, "There is an empty value");
  72. }
  73. $fieldName = $fields[$i];
  74.  
  75. if ( ! $fieldName) continue;
  76.  
  77. $this->{$line}->$fields[$i] = $val;
  78.  
  79. $i++;
  80. }
  81. }
  82. }
  83.  
  84. public function log()
  85. {
  86. return $this->log;
  87. }
  88.  
  89. private function line_values(&$line)
  90. {
  91. $line = trim($line);
  92.  
  93. $values = preg_split('/"([ \t]+)?,([ \t]+)?"/', $line);
  94.  
  95. foreach ($values as &$val)
  96. {
  97. $val = trim($val, '"');
  98. }
  99. return $values;
  100. }
  101.  
  102. private function log_message($line, $message)
  103. {
  104. $this->log[] = "Line $line: $message";
  105. }
  106.  
  107. }
  108.  
  109. /*
  110. Example.txt:
  111. "firstname","surname"
  112. "Jane","Doe"
  113. "John","Doe"
  114. */
  115.  
  116. $csv = new CSVreader('Example.txt');
  117. foreach ($csv as $line => $row) {
  118. echo "$line: {$row->firstname} {$row->surname}<br />\n";
  119. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Sverri on October 11, 2010

PHP is extremely flexible when it comes to key, variable and field (member) names. They can be called just about anything, provided you enclose it in curly braces, such as ${"this is a variable"} = "this works";. Likewise, if you have fields with spaces in them, you can access them using curly braces, like this: $row->{"a field name"};.

Posted By: Sverri on October 11, 2010

PHP is extremely flexible when it comes to key, variable and field (member) names. They can be called just about anything, provided you enclose it in curly braces, such as ${"this is a variable"} = "this works";. Likewise, if you have fields with spaces in them, you can access them using curly braces, like this: $row->{"a field name"};.

You need to login to post a comment.