note 91051 added to function.mb-detect-encoding
| From: | nat3738 at gmail dot com | Date: | Fri, 22 May 2009 10:58:04 +0000 |
| Subject: | note 91051 added to function.mb-detect-encoding | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-155873@lists.php.net to get a copy of this message | ||
A simple way to detect UTF-8/16/32 of file by its BOM (not work with string or file without BOM)
<?php
// Unicode BOM is U+FEFF, but after encoded, it will look like this.
define ('UTF32_BIG_ENDIAN_BOM' , chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));
define ('UTF32_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));
define ('UTF16_BIG_ENDIAN_BOM' , chr(0xFE) . chr(0xFF));
define ('UTF16_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE));
define ('UTF8_BOM' , chr(0xEF) . chr(0xBB) . chr(0xBF));
function detect_utf_encoding($filename) {
$text = file_get_contents($filename);
$first2 = substr($text, 0, 2);
$first3 = substr($text, 0, 3);
$first4 = substr($text, 0, 3);
if ($first3 == UTF8_BOM) return 'UTF-8';
elseif ($first4 == UTF32_BIG_ENDIAN_BOM) return 'UTF-32BE';
elseif ($first4 == UTF32_LITTLE_ENDIAN_BOM) return 'UTF-32LE';
elseif ($first2 == UTF16_BIG_ENDIAN_BOM) return 'UTF-16BE';
elseif ($first2 == UTF16_LITTLE_ENDIAN_BOM) return 'UTF-16LE';
}
?>
----
Server IP: 203.121.145.220
Probable Submitter: 58.8.252.58
----
Manual Page -- http://www.php.net/manual/en/function.mb-detect-encoding.php
Edit -- https://master.php.net/note/edit/91051
Del: integrated -- https://master.php.net/note/delete/91051/integrated
Del: useless -- https://master.php.net/note/delete/91051/useless
Del: bad code -- https://master.php.net/note/delete/91051/bad+code
Del: spam -- https://master.php.net/note/delete/91051/spam
Del: non-english -- https://master.php.net/note/delete/91051/non-english
Del: in docs -- https://master.php.net/note/delete/91051/in+docs
Del: other reasons-- https://master.php.net/note/delete/91051
Reject -- https://master.php.net/note/reject/91051
Search -- https://master.php.net/manage/user-notes.php