Skip to content

Instantly share code, notes, and snippets.

@blackymetal
Created May 31, 2019 03:11
Show Gist options
  • Save blackymetal/eaa568635d4660cf92dc6e6e6ddab93b to your computer and use it in GitHub Desktop.
Save blackymetal/eaa568635d4660cf92dc6e6e6ddab93b to your computer and use it in GitHub Desktop.
CSV reader
John Doe 120 jefferson st. Riverside NJ 08075
Jack McGinnis 220 hobo Av. Phila PA 09119
John "Da Man" Repici 120 Jefferson St. Riverside NJ 08075
Stephen Tyler 7452 Terrace "At the Plaza" road SomeTown SD 91234
Blankman SomeTown SD 00298
Joan "the bone", Anne Jet 9th, at Terrace plc Desert City CO 00123
<?php
namespace Csv;
class Csv
{
private $path = null;
private $fh = null;
public function __construct(string $path = null)
{
$this->setPath($path);
}
public function setPath(string $path): void
{
$this->path = $path;
$this->open();
}
public function readLine()
{
return $this->getStringLine();
}
public function getStringLine()
{
if (($row = fgetcsv($this->fh)) !== false) {
return $row;
}
return false;
}
private function open(): bool
{
if (($fh = fopen($this->path, 'r')) !== false) {
$this->fh = $fh;
return true;
}
return false;
}
}
<?php
require('Csv.php');
use Csv\Csv;
$csv = new Csv('./addresses.csv');
while ($line = $csv->readLine()) {
print_r($line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment