如何在PHP中将Unicode字符转换为Unicode十进制实体?

3
我在MySQL表中有Unicode字符。我将在网页中打印数据。在页面上打印时,我会动态生成“分享此”按钮,以分享该表中的每个记录(该记录使用旁遮普语)。
因此,页面输出看起来很好。但是,在“分享此”中共享相同内容时,目标页面显示一些未知字符。后来我发现,发送到网站的数据应该是Unicode实体格式(例如ਆ这将打印出)。
现在我的表格具有像ਜ ਝ ਞ ਟ ਠ ਡ ਢ这样的值。
我想将这些数据转换为PHP中的ਜ ਝ ਞ ਟ ਠ ਡ ਢ
请帮助我解决这个问题。
上述问题已解决。但是,“分享此”仍然在显示Unicode字符时出现问题。以下是浏览器的输出:
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">
stLight.options({
    publisher:'12345'
});
</script>
<span class="st_facebook" st_title="ੴਸਤਿ ਨਾਮੁ ਕਰਤਾ ਪੁਰਖੁ ਨਿਰਭਉ ਨਿਰਵੈਰੁ ਅਕਾਲ ਮੂਰਤਿ ਅਜੂਨੀ ਸੈਭੰ ਗੁਰ ਪ੍ਰਸਾਦਿ ॥" st_url="http://sitelink/"></span>
提前感谢, 阿布·西蒂克

请问您能否编辑您的问题以使实体可见?SO会自动将它们替换为普通字符;请使用反引号将它们包含起来以防止这种情况:&#1234; - Philipp
谢谢朋友。我从这个页面 http://www.php.net/manual/en/function.htmlentities.php#96648 中使用了 utf8tohtml($utf8, $encodeTags) 方法得到了解决方案。但问题是现在的 'Share This' 没有分享实体本身。 :( - Abu Sithik
2个回答

3

前一段时间,我为缺少多字节版本的 ordchr 编写了一个 polyfill,其中考虑了以下内容:

  • 仅在函数 mb_ordmb_chr 不存在时定义它们。如果它们已经存在于您的框架或 PHP 的某个未来版本中,则会忽略这个 polyfill。

  • 使用广泛使用的 mbstring 扩展进行转换。如果未加载 mbstring 扩展,则将使用 iconv 扩展。

我还添加了用于 HTML 实体编码 / 解码以及编码 / 解码为 JSON 格式的函数,以及一些演示如何使用这些函数的示例代码。


代码

if (!function_exists('codepoint_encode')) {

    function codepoint_encode($str) {
        return substr(json_encode($str), 1, -1);
    }

}

if (!function_exists('codepoint_decode')) {

    function codepoint_decode($str) {
        return json_decode(sprintf('"%s"', $str));
    }

}

if (!function_exists('mb_internal_encoding')) {

    function mb_internal_encoding($encoding = NULL) {
        return ($from_encoding === NULL) ? iconv_get_encoding() : iconv_set_encoding($encoding);
    }

}

if (!function_exists('mb_convert_encoding')) {

    function mb_convert_encoding($str, $to_encoding, $from_encoding = NULL) {
        return iconv(($from_encoding === NULL) ? mb_internal_encoding() : $from_encoding, $to_encoding, $str);
    }

}

if (!function_exists('mb_chr')) {

    function mb_chr($ord, $encoding = 'UTF-8') {
        if ($encoding === 'UCS-4BE') {
            return pack("N", $ord);
        } else {
            return mb_convert_encoding(mb_chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
        }
    }

}

if (!function_exists('mb_ord')) {

    function mb_ord($char, $encoding = 'UTF-8') {
        if ($encoding === 'UCS-4BE') {
            list(, $ord) = (strlen($char) === 4) ? @unpack('N', $char) : @unpack('n', $char);
            return $ord;
        } else {
            return mb_ord(mb_convert_encoding($char, 'UCS-4BE', $encoding), 'UCS-4BE');
        }
    }

}

if (!function_exists('mb_htmlentities')) {

    function mb_htmlentities($string, $hex = true, $encoding = 'UTF-8') {
        return preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($match) use ($hex) {
            return sprintf($hex ? '&#x%X;' : '&#%d;', mb_ord($match[0]));
        }, $string);
    }

}

if (!function_exists('mb_html_entity_decode')) {

    function mb_html_entity_decode($string, $flags = null, $encoding = 'UTF-8') {
        return html_entity_decode($string, ($flags === NULL) ? ENT_COMPAT | ENT_HTML401 : $flags, $encoding);
    }

}

如何使用

echo "\nGet string from numeric DEC value\n";
var_dump(mb_chr(25105));
var_dump(mb_chr(22909));

echo "\nGet string from numeric HEX value\n";
var_dump(mb_chr(0x6211));
var_dump(mb_chr(0x597D));

echo "\nGet numeric value of character as DEC int\n";
var_dump(mb_ord('我'));
var_dump(mb_ord('好'));

echo "\nGet numeric value of character as HEX string\n";
var_dump(dechex(mb_ord('我')));
var_dump(dechex(mb_ord('好')));

echo "\nEncode / decode to DEC based HTML entities\n";
var_dump(mb_htmlentities('我好', false));
var_dump(mb_html_entity_decode('&#25105;&#22909;'));

echo "\nEncode / decode to HEX based HTML entities\n";
var_dump(mb_htmlentities('我好'));
var_dump(mb_html_entity_decode('&#x6211;&#x597D;'));

echo "\nUse JSON encoding / decoding\n";
var_dump(codepoint_encode("我好"));
var_dump(codepoint_decode('\u6211\u597d'));

输出

Get string from numeric DEC value
string(3) "我"
string(3) "好"

Get string from numeric HEX value
string(3) "我"
string(3) "好"

Get numeric value of character as DEC string
int(25105)
int(22909)

Get numeric value of character as HEX string
string(4) "6211"
string(4) "597d"

Encode / decode to DEC based HTML entities
string(16) "&#25105;&#22909;"
string(6) "我好"

Encode / decode to HEX based HTML entities
string(16) "&#x6211;&#x597D;"
string(6) "我好"

Use JSON encoding / decoding
string(12) "\u6211\u597d"
string(6) "我好"

-1

在输出 <a> 标签的 href 时,你应该使用 url_encode 对文本进行编码。


谢谢朋友。我从这个页面 http://www.php.net/manual/en/function.htmlentities.php#96648 中使用了 utf8tohtml($utf8, $encodeTags) 方法得到了解决方案。但问题是现在的“分享此内容”没有共享实体本身。 :( - Abu Sithik
@Abu 不用谢 :) 你能否编辑你的问题,添加输出分享按钮的 PHP 代码和它的 HTML 输出(在浏览器中的页面源代码)? - aularon
完成。在st_title属性中显示的Gurmukhi(旁遮普语)字符实际上是像&#2676;&#2616;这样的,而不是在此处显示的样子。 - Abu Sithik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接