php - 如何将 unicode 字符转换为 unicode 十进制实体 php?

标签 php unicode html-entities sharethis

我在 MySQL 表中有 unicode 字符。我将打印网页中的数据。在页面中打印时,我动态生成“共享此”按钮以共享该表(使用旁遮普语)中的每条记录。

因此页面中的输出看起来不错。但是,在“分享此内容”中分享相同内容时,目标页面会显示一些未知字符。后来我发现通过网站发送的数据应该是Unicode实体格式(比如这将打印这个'')。

现在我的表有像 ਜ ਝ ਞ ਟ ਠ ਡ ਢ 这样的值。

我想像 ਜ 那样转换这些数据ਝ ਞ ਟ ਠ ਡ ਢ 在 PHP 中。

请帮助我。


以上问题已解决。但是 Share This 在显示 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>

提前致谢, 阿布锡提克

最佳答案

前一段时间,我写了一个 polyfill 用于缺少 ordchr 的多字节版本,考虑到以下几点:

  • 它仅在函数 mb_ordmb_chr 尚不存在时才定义它们。如果它们确实存在于您的框架或某些 future 版本的 PHP 中,polyfill 将被忽略。

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

我还添加了 HTMLentities 编码/解码和编码/解码为 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) "我好"

关于php - 如何将 unicode 字符转换为 unicode 十进制实体 php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3657233/

相关文章:

php - 为什么 PHP 函数 htmlentities(...) 返回错误结果?

ruby - 为什么我会看到这两个几乎相同的 Ruby 正则表达式模式的不同结果,为什么一个匹配我认为不应该匹配的内容?

c# - 如何读取ANSI编码非英文字母的文本文件?

java - 在 XML 中传递 unicode 字符

php - PHP 中 HTML 的 Substr 字符串

html - 为什么 Twitter 和 Google API 文档不对 URL 中的 & 符号进行编码?

php - 存储过程中的准备语句是否可以避免 SQL 注入(inject)?

php - 如何复制 mysql 表,添加一些列并删除一些列,更改顺序并保留与新表相关的任何数据?

php - jeditable、pdo 和 mysql

php - 使用 AJAX 将数据发送到数据库失败