Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created September 16, 2016 12:46
Show Gist options
  • Select an option

  • Save mneuhaus/7cb6089a30f8d7b4b9cefd4284cfe73c to your computer and use it in GitHub Desktop.

Select an option

Save mneuhaus/7cb6089a30f8d7b4b9cefd4284cfe73c to your computer and use it in GitHub Desktop.
<?php
function determineEncoding($file)
{
$string = file_get_contents($file);
$encodingCandidates = array(
'UTF-8',
'ASCII',
'macintosh',
'ISO-8859-1',
'ISO-8859-2',
'ISO-8859-3',
'ISO-8859-4',
'ISO-8859-5',
'ISO-8859-6',
'ISO-8859-7',
'ISO-8859-8',
'ISO-8859-9',
'ISO-8859-10',
'ISO-8859-13',
'ISO-8859-14',
'ISO-8859-15',
'ISO-8859-16',
'Windows-1251',
'Windows-1252',
'Windows-1254',
);
$result = false;
foreach ($encodingCandidates as $item) {
$sample = iconv($item, $item, $string);
if (md5($sample) == md5($string)) {
$result = $item;
break;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment