For useful function added by jbricci at ya-right dot com
<?php
function checkEncoding ( $string, $string_encoding )
{
$fs = $string_encoding == 'UTF-8' ? 'UTF-32' : $string_encoding;
$ts = $string_encoding == 'UTF-32' ? 'UTF-8' : $string_encoding;
return $string === mb_convert_encoding ( mb_convert_encoding ( $string, $fs, $ts ), $ts, $fs );
}
?>
I've made a function that is guessing the codepage:
<?php
function detectEncoding($string)
{
$arr_encodings = [
'CP1251',
'UCS-2LE',
'UCS-2BE',
'UTF-8',
'UTF-16',
'UTF-16BE',
'UTF-16LE',
'CP866',
];
foreach($arr_encodings as $encoding){
if (checkEncoding($string, $encoding)){
return $encoding;
}
}
return false;
}
?>