【问题标题】:UTF-8 Hex code to UnicodeUTF-8 十六进制代码到 Unicode
【发布时间】:2015-12-06 15:14:27
【问题描述】:

假设我有俄语西里尔字母“журнал”的字符串,它具有以下十六进制代码:

d0 b6 d1 83 d1 80 d0 bd d0 b0 d0 bb

但是如何将这个十六进制代码转换回人类可读的西里尔字符串在 PHP 中?我查了很多资料,到目前为止都没有任何效果。

【问题讨论】:

    标签: php unicode utf-8 hex


    【解决方案1】:

    你可以用这个:

    function hex2str($hex) {
        $str = '';
        for($i = 0; $i < strlen($hex); $i += 2) {
            $str .= chr(hexdec(substr($hex, $i, 2)));
        }
        return $str;
    }
    
    $string = preg_replace('/\s+/', '', 'd0 b6 d1 83 d1 80 d0 bd d0 b0 d0 bb');
    
    echo hex2str($string);
    

    【讨论】:

    • 这行得通,谢谢。之前看到过类似的功能,不知道需要去掉空格。
    猜你喜欢
    • 2021-10-20
    • 2020-02-06
    • 2014-08-07
    • 2013-04-12
    • 1970-01-01
    • 2011-06-10
    • 2012-02-25
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多