Correctly check for BOM and skip if exists

No characters are skipped if BOM doesn't exist
This commit is contained in:
objecttothis
2019-11-18 14:04:07 +04:00
committed by travi
parent a26ae1a9b3
commit 0180de5490

View File

@@ -62,6 +62,12 @@ function get_csv_file($file_name)
if(($csv_file = fopen($file_name,'r')) !== FALSE)
{
//Skip Byte-Order Mark
if(bom_exists($csv_file) === TRUE)
{
fseek($csv_file, 3);
}
while (($data = fgetcsv($csv_file)) !== FALSE)
{
$line_array[] = $data;
@@ -74,4 +80,27 @@ function get_csv_file($file_name)
return $line_array;
}
/**
* Checks the first three characters of a file for the Byte-Order Mark then returns the file position to the first character.
*
* @param object $file_handle File handle to check
* @return bool Returns TRUE if the BOM exists and FALSE otherwise.
*/
function bom_exists(&$file_handle)
{
$str = fread($file_handle,3);
rewind($file_handle);
$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 === strncmp($str, $bom, 3))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>