php - 将所有符号转换为 html 实体

标签 php character-encoding special-characters html-entities html-encode

在使用内置函数的 PHP 中,似乎不包含特殊符号和新符号。 ALL 包括 3 个月前发布的那些。想要转换带有混合符号的字符串,例如:

𝕃𝕆𝕃 𝔯𝔬𝔠𝔰 𝓂𝓎 δϱ ☎

进入

𝕃𝕆𝕃 𝔯𝔬𝔠𝔰 𝓂𝓎 δϱж ☎

(浏览器将呈现相同的内容)

我看到这是即时完成的。我们在这里谈论无数的符号。谁知道将来还会有多少。

他们是如何实现这一目标的?他们不可能真的拥有包含每个符号及其实体的 1000 多个键数组吗?

我已经回答了所有相关问题,到目前为止还没有成功。

最佳答案

此函数会将除 [0-9A-Za-z ] 之外的每个字符(当前和 future )转换为数字实体。假定 UTF-8 字符编码:

function html_entity_encode_all($s) {
    $out = '';
    for ($i = 0; isset($s[$i]); $i++) {
        // read UTF-8 bytes and decode to a Unicode codepoint value:
        $x = ord($s[$i]);
        if ($x < 0x80) {
            // single byte codepoints
            $codepoint = $x;
        } else {
            // multibyte codepoints
            if ($x >= 0xC2 && $x <= 0xDF) {
                $codepoint = $x & 0x1F;
                $length = 2;
            } else if ($x >= 0xE0 && $x <= 0xEF) {
                $codepoint = $x & 0x0F;
                $length = 3;
            } else if ($x >= 0xF0 && $x <= 0xF4) {
                $codepoint = $x & 0x07;
                $length = 4;
            } else {
                // invalid byte
                $codepoint = 0xFFFD;
                $length = 1;
            }
            // read continuation bytes of multibyte sequences:
            for ($j = 1; $j < $length; $j++, $i++) {
                if (!isset($s[$i + 1])) {
                    // invalid: string truncated in middle of multibyte sequence
                    $codepoint = 0xFFFD;
                    break;
                }
                $x = ord($s[$i + 1]);
                if (($x & 0xC0) != 0x80) {
                    // invalid: not a continuation byte
                    $codepoint = 0xFFFD;
                    break;
                }
                $codepoint = ($codepoint << 6) | ($x & 0x3F);
            }
            if (($codepoint > 0x10FFFF) ||
                ($length == 2 && $codepoint < 0x80) ||
                ($length == 3 && $codepoint < 0x800) ||
                ($length == 4 && $codepoint < 0x10000)) {
                // invalid: overlong encoding or out of range
                $codepoint = 0xFFFD;
            }
        }

        // have codepoint, now output:
        if (($codepoint >= 48 && $codepoint <= 57) ||
            ($codepoint >= 65 && $codepoint <= 90) ||
            ($codepoint >= 97 && $codepoint <= 122) ||
            ($codepoint == 32)) {
            // leave plain 0-9, A-Z, a-z, and space unencoded
            $out .= $s[$i];
        } else {
            // all others as numeric entities
            $out .= '&#' . $codepoint . ';';
        }
    }
    return $out;
}

对于解码,可以使用标准函数html_entity_decode

关于php - 将所有符号转换为 html 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645675/

相关文章:

php - 警告 : preg_replace(): Unknown modifier 'g'

php - Codeigniter 消息 : Parameter 3 to mysqli_stmt_bind_param() expected to be a reference, 给出的值 -- stmt_send_long_data

javascript - 如何将数组中多个 1 元素的值从 ajax 传递到 PHP?

javascript - 如何屏蔽特殊字符?

css - 编译 LESS 时导致问题的特殊字符

php - 为什么此代码会导致Expected “)”错误?

mysql - 离线表情符号/表情显示??? MySql中的Offline表

mysql - UTF-8:在数据库中正确显示,但在 HTML 中不正确,尽管 utf-8 字符集

c# - 将特殊字符转换为普通字符

ruby - 在 Ruby 中读取带有特殊字符的 CSV 文件并存储到 SQL Server 中