您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 检测资料编码类型

2024/5/20 9:19:33发布32次查看
php 检测文件编码类型
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($text) {
    $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';
}
function getfileencoding($str){
    $encoding=mb_detect_encoding($str);
    if(empty($encoding)){
        $encoding=detect_utf_encoding($str);
    }
    return $encoding;
}
$file = 'text1.txt';
echo getfileencoding(file_get_contents($file));  // 输出ascii
echo '';
$file = 'text2.txt';
echo getfileencoding(file_get_contents($file));  // 输出utf-8
echo '';
$file = 'text3.txt';
echo getfileencoding(file_get_contents($file));  // 输出utf-16le
echo '';
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product