Skip to content

Instantly share code, notes, and snippets.

@vasilcov77
Created September 20, 2021 10:29
Show Gist options
  • Save vasilcov77/b3ff504c0e6dd688a21f7db19a996d46 to your computer and use it in GitHub Desktop.
Save vasilcov77/b3ff504c0e6dd688a21f7db19a996d46 to your computer and use it in GitHub Desktop.
File csv
class CsvFile
{
private $file;
private $delimiter = "\t";
public function __construct(string $file)
{
$this->file = $file;
}
public function parse($code)
{
$result['double'] = false;
if (($file = fopen($this->file, "r")) !== FALSE) {
while (($data = fgetcsv($file, 100, $this->delimiter)) !== FALSE) {
if (current($data) === $code) {
$result['double'] = true;
fclose($file);
break;
}
}
fclose($file);
}
$result['handle'] = $file;
return $result;
}
public function getNumRows()
{
$count = 0;
if (($file = fopen($this->file, "r")) !== FALSE) {
while (($data = fgetcsv($file, 100, $this->delimiter)) !== FALSE) {
if (!empty(current($data))) {
$count++;
}
}
fclose($file);
}
return $count;
}
public function save($code)
{
$file = fopen($this->file, 'a+');
fputcsv($file, [$code], $this->delimiter);
fclose($file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment