【问题标题】:convert unicode to html entities hex将 unicode 转换为 html 实体十六进制
【发布时间】:2012-10-28 03:03:48
【问题描述】:

如何将 Unicode 字符串转换为 HTML 实体? (HEX 不是十进制)

例如,将Français 转换为Français

【问题讨论】:

  • 你需要这个做什么?它不应该永远没有必要......
  • 这取决于具体的 unicode 编码。 mb_convert_encoding($utf_8, 'HTML-ENTITIES', 'UTF-8'); 例如适用于 PHP 中的 UTF-8 unicode 字符串。如果您 需要 十六进制编码,链接的答案将向您展示如何捕获所有这些(来自 utf-8 字符串),您只需要运行您的十六进制编码。
  • @hakre:字符串是UTF-8mb_convert_encoding($utf_8, 'HTML-ENTITIES', 'UTF-8'); 转换为十进制,但我想要hex 代码。
  • 你的问题不是很准确。我想如果我做对了,输出是Français 而不是Français
  • @Pekka웃 - 我刚刚在 2015 年发现了一个供应商 API,它需要纯 US-ASCII XML 请求来处理与 Unicode 相关的功能。 叹息

标签: php unicode hex html-entities


【解决方案1】:

您也可以使用 PHP 4.0.6+ (link to PHP doc) 支持的mb_encode_numericentity

function unicode2html($value) {
    return mb_encode_numericentity($value, [
    //  start codepoint
    //  |       end codepoint
    //  |       |       offset
    //  |       |       |       mask
        0x0000, 0x001F, 0x0000, 0xFFFF,
        0x0021, 0x002C, 0x0000, 0xFFFF,
        0x002E, 0x002F, 0x0000, 0xFFFF,
        0x003C, 0x003C, 0x0000, 0xFFFF,
        0x003E, 0x003E, 0x0000, 0xFFFF,
        0x0060, 0x0060, 0x0000, 0xFFFF,
        0x0080, 0xFFFF, 0x0000, 0xFFFF
    ], 'UTF-8', true);
}

通过这种方式,还可以指示将哪些字符范围转换为十六进制实体,以及将哪些字符保留为字符。

使用示例:

$input = array(
    '"Meno più, PIÙ o meno"',
    '\'ÀÌÙÒLÈ PERCHÉ perché è sempre così non si sà\'',
    '<script>alert("XSS");</script>',
    '"`'
);

$output = array();
foreach ($input as $str)
    $output[] = unicode2html($str)

结果:

$output = array(
    '&#x22;Meno pi&#xF9;&#x2C; PI&#xD9; o meno&#x22;',
    '&#x27;&#xC0;&#xCC;&#xD9;&#xD2;L&#xC8; PERCH&#xC9; perch&#xE9; &#xE8; sempre cos&#xEC; non si s&#xE0;&#x27;',
    '&#x3C;script&#x3E;alert&#x28;&#x22;XSS&#x22;&#x29;;&#x3C;&#x2F;script&#x3E;',
    '&#x22;&#x60;'
);

【讨论】:

    【解决方案2】:

    请参阅How to get the character from unicode code point in PHP? 获取一些允许您执行以下操作的代码:

    使用示例

    echo "Get string from numeric DEC value\n";
    var_dump(mb_chr(50319, 'UCS-4BE'));
    var_dump(mb_chr(271));
    
    echo "\nGet string from numeric HEX value\n";
    var_dump(mb_chr(0xC48F, 'UCS-4BE'));
    var_dump(mb_chr(0x010F));
    
    echo "\nGet numeric value of character as DEC string\n";
    var_dump(mb_ord('ď', 'UCS-4BE'));
    var_dump(mb_ord('ď'));
    
    echo "\nGet numeric value of character as HEX string\n";
    var_dump(dechex(mb_ord('ď', 'UCS-4BE')));
    var_dump(dechex(mb_ord('ď')));
    
    echo "\nEncode / decode to DEC based HTML entities\n";
    var_dump(mb_htmlentities('tchüß', false));
    var_dump(mb_html_entity_decode('tch&#252;&#223;'));
    
    echo "\nEncode / decode to HEX based HTML entities\n";
    var_dump(mb_htmlentities('tchüß'));
    var_dump(mb_html_entity_decode('tch&#xFC;&#xDF;'));
    
    echo "\nUse JSON encoding / decoding\n";
    var_dump(codepoint_encode("tchüß"));
    var_dump(codepoint_decode('tch\u00fc\u00df'));
    

    输出

    Get string from numeric DEC value
    string(4) "ď"
    string(2) "ď"
    
    Get string from numeric HEX value
    string(4) "ď"
    string(2) "ď"
    
    Get numeric value of character as DEC int
    int(50319)
    int(271)
    
    Get numeric value of character as HEX string
    string(4) "c48f"
    string(3) "10f"
    
    Encode / decode to DEC based HTML entities
    string(15) "tch&#252;&#223;"
    string(7) "tchüß"
    
    Encode / decode to HEX based HTML entities
    string(15) "tch&#xFC;&#xDF;"
    string(7) "tchüß"
    
    Use JSON encoding / decoding
    string(15) "tch\u00fc\u00df"
    string(7) "tchüß"
    

    【讨论】:

      【解决方案3】:

      首先,当我最近遇到这个问题时,我通过确保我的代码文件、数据库连接和数据库表都是 UTF-8 来解决它然后,简单地回显文本即可。如果您必须转义 DB 的输出,请使用 htmlspecialchars() 而不是 htmlentities(),这样 UTF-8 符号就不会被尝试转义。

      想记录一个替代解决方案,因为它为我解决了类似的问题。 我使用 PHP 的 utf8_encode() 来转义“特殊”字符。

      我想将它们转换为 HTML 实体以进行显示,我编写此代码是因为我想尽可能避免使用 iconv 或此类功能,因为并非所有环境都必须具有它们(如果不是这样,请纠正我!)

      $foo = 'This is my test string \u03b50';
      echo unicode2html($foo);
      
      function unicode2html($string) {
          return preg_replace('/\\\\u([0-9a-z]{4})/', '&#x$1;', $string);
      }
      

      希望这可以帮助有需要的人:-)

      【讨论】:

        【解决方案4】:

        你的字符串看起来像UCS-4编码你可以试试

        $first = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) {
            $char = current($m);
            $utf = iconv('UTF-8', 'UCS-4', $char);
            return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0"));
        }, $string);
        

        输出

        string 'Fran&#xE7;ais' (length=13)
        

        【讨论】:

        • 如果我想将Fran 转换为十六进制,我应该使用哪种编码?
        • echo bin2hex("Fran"); .. 你不需要编码
        【解决方案5】:

        related question 中缺少的十六进制编码:

        $output = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($match) {
            list($utf8) = $match;
            $binary = mb_convert_encoding($utf8, 'UTF-32BE', 'UTF-8');
            $entity = vsprintf('&#x%X;', unpack('N', $binary));
            return $entity;
        }, $input);
        

        这类似于@Baba 的答案,使用UTF-32BE,然后使用unpackvsprintf 来满足格式化需求。

        如果您更喜欢iconv 而不是mb_convert_encoding,则类似:

        $output = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($match) {
            list($utf8) = $match;
            $binary = iconv('UTF-8', 'UTF-32BE', $utf8);
            $entity = vsprintf('&#x%X;', unpack('N', $binary));
            return $entity;
        }, $input);
        

        我发现这个字符串操作比Get hexcode of html entities 更清晰。

        【讨论】:

        • 太棒了!我用它来编码 CKEditors 输出,将我的 html 实体转换为 unicode 符号。
        • 这帮助我在 ISO-8859-1 网站上显示表情符号。首先,我使用这种方法转换为十六进制,然后我可以将其保存在数据库中并在网站和应用程序中的 web 视图中显示。非常好。
        猜你喜欢
        • 2011-03-29
        • 2021-04-30
        • 1970-01-01
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        • 2012-07-29
        • 2015-04-11
        相关资源
        最近更新 更多