将字符串清理为UTF-8的最佳PHP方法/类是什么?

时间:2012-04-30 15:25:35

标签: php utf-8 character-encoding

我一直在一个简单的类上使用了几种方法,这些方法对我来说没问题,但是我注意到它们由于strtr()而非常慢并且定义了大量的翻译。它也很长,因此更难以维护和理解。

也就是说,所有“坏”的例子都是现实世界问题的解决方案,将字符串翻译成UTF8。

有谁能告诉我有一个众所周知的或更有效的方法吗? (是的,我尝试了htmlentities()方法和iconv()方法,但都没有真正替换所有时髦字符。

这是我目前正在使用的课程: https://gist.github.com/2559140

1 个答案:

答案 0 :(得分:2)

从PHP 5.4.0开始,默认情况下已启用(但未加载)mbstring支持。加载扩展程序,您可以执行以下操作:

<? //PHP 5.4+
$ensureIsUTF8 = static function($data){
    $dataEncoding = \mb_detect_encoding(
        $data,
        ['UTF-8', 'windows-1251', 'iso-8859-1', /*others you encounter*/],
        true
    );

    //UTF-16/32 encoding detection always fails for PHP <= 5.4.1
    //Use detection code copied from PHP docs comments:
    //http://www.php.net/manual/en/function.mb-detect-encoding.php
    if ($dataEncoding === false){

        $UTF32_BIG_ENDIAN_BOM = chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF);
        $UTF32_LITTLE_ENDIAN_BOM = chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00);
        $UTF16_BIG_ENDIAN_BOM = chr(0xFE) . chr(0xFF);
        $UTF16_LITTLE_ENDIAN_BOM = chr(0xFF) . chr(0xFE);

        $first2 = \substr($data, 0, 2);
        $first4 = \substr($data, 0, 4);

        if ($first4 === $UTF32_BIG_ENDIAN_BOM) {
            $dataEncoding = 'UTF-32BE';
        } elseif ($first4 === $UTF32_LITTLE_ENDIAN_BOM) {
            $dataEncoding = 'UTF-32LE';
        } elseif ($first2 === $UTF16_BIG_ENDIAN_BOM) {
            $dataEncoding = 'UTF-16BE';
        } elseif ($first2 === $UTF16_LITTLE_ENDIAN_BOM) {
            $dataEncoding = 'UTF-16LE';
        } else {
            throw new \Exception('Whoa! No idea what that was.');
        }
    }

    if ($dataEncoding === 'UTF-8'){
        return $data;
    } else {
        return \mb_convert_encoding(
           $data,
           'UTF-8',
           $dataEncoding
        );      
    }
};

$utf8Data = $ensureIsUTF8(\file_get_contents('something'));
$utf8Data = $ensureIsUTF8(\file_get_contents('http://somethingElse'));
$utf8Data = $ensureIsUTF8($userProvidedData);
?>