Skip to content

Instantly share code, notes, and snippets.

@mikkohei13
Created November 27, 2012 11:09
Show Gist options
  • Save mikkohei13/4153678 to your computer and use it in GitHub Desktop.
Save mikkohei13/4153678 to your computer and use it in GitHub Desktop.
Checks if file is UTF-8 and without BOM
/*
Thanks to
http://www.php.net/manual/en/function.mb-detect-encoding.php#91051
http://stackoverflow.com/questions/8907196/check-if-csv-file-is-in-utf-8-with-php
*/
if (! mb_check_encoding($data, 'UTF-8')) {
exit("Not UTF-8");
}
else
{
define ('UTF8_BOM' , chr(0xEF) . chr(0xBB) . chr(0xBF));
$first3 = substr($data, 0, 3);
if ($first3 == UTF8_BOM)
{
exit("UTF-8 with BOM");
}
else
{
exit("UTF-8 without BOM");
}
}
@Eolia
Copy link

Eolia commented Jul 15, 2021

$data = substr($data, 3);
:))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment