【问题标题】:How to Convert UTF-16 hexadecimal string to UTF-8 in PHP?如何在 PHP 中将 UTF-16 十六进制字符串转换为 UTF-8?
【发布时间】:2010-11-16 08:22:46
【问题描述】:

我有以下来自 strace 的输出,我想使用 PHP 将其转换为 UTF-8:

R\00f6dhakev\00e4gen 4
R\00e4ntm\00e4starv\00e4gen 24
K\00d8BENHAVN

我认为上面的字符串是 UTF 16 HEX。

【问题讨论】:

    标签: php utf-8 utf-16


    【解决方案1】:

    发现以下功能有效:

    function utf8_urldecode($str) {
    
      $str = str_replace("\\00", "%u00", $str);
    
      $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
    
      return html_entity_decode($str,null,'UTF-8');
    
    }
    

    部分来自http://us2.php.net/manual/en/function.urldecode.php

    【讨论】:

      【解决方案2】:

      试试这个:

      function masked_utf16_to_utf8($str) {
          $str = preg_replace_callback('/\\\\([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})/', create_function('$match', 'return mb_convert_encoding(chr(hexdec("$match[1]")).chr(hexdec("$match[2]")), "UTF-8", "UTF-16");');
          return $str;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 2020-02-06
        相关资源
        最近更新 更多