note 122892 deleted from function.mb-check-encoding by salathe
| From: | salathe@php.net | Date: | Sat, 14 Aug 2021 09:08:36 +0000 |
| Subject: | note 122892 deleted from function.mb-check-encoding by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216789@lists.php.net to get a copy of this message | ||
Note Submitter: dorbah NOSPAM at rambler [dot] ru
----
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;
}
?>