Created
May 31, 2019 03:11
-
-
Save blackymetal/eaa568635d4660cf92dc6e6e6ddab93b to your computer and use it in GitHub Desktop.
CSV reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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