Hey guys I've seen a lot of options on fread (which requires a fiole, or writing to memory), but I am trying to invalidate an input based on a string that has already been accepted (unknown format). I have something like this
if (FALSE !== str_getcsv($this->_contents, "\n"))
{
foreach (preg_split("/\n/", $this->_contents) AS $line)
{
$data[] = explode(',', $line);
}
print_r($data); die;
$this->_format = 'csv';
$this->_contents = $this->trimContents($data);
return true;
}
Which works fine on a real csv or csv filled variable, but when I try to pass it garbage to invalidate, something like: https://www.gravatar.com/avatar/625a713bbbbdac8bea64bb8c2a9be0a4 which is garbage (since its a png), it believes its csv anyway and keeps on chugging along until the program chokes. How can I fix this? I have not seen and CSV validators that are not at least several classes deep, is there a simple three or four line to (in)validate?
is there a simple three or four line to (in)validate?nope. CSV is so loosely structured (and it has no telltale signs like header bytes) that there technically is no way to tell whether a file is CSV or not.