3

我在 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({
    发布者:'12345'
});
</脚本>
<span class =“st_facebook”st_title =“ੴਸਤਿੴਸਤਿਨਾਮੁਕਰਤਾਪੁਰਖੁਨਿਰਭਉਨਿਰਵੈਰੁਅਕਾਲਮੂਰਤਿਅਜੂਨੀਸੈਭੰਗੁਰਪ੍ਰਸਾਦਿਪ੍ਰਸਾਦਿਮੂਰਤਿਅਜੂਨੀਸੈਭੰਸੈਭੰਪ੍ਰਸਾਦਿ。” st_url="http://sitelink/"></span>

提前致谢, 阿布西提克

4

2 回答 2

3

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

  • 它定义了函数mb_ord并且mb_chr仅当它们不存在时。如果它们确实存在于您的框架或某些未来版本的 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) "我好"
于 2015-11-25T00:19:33.453 回答
-1

您可能应该在输出标签url_encode时使用文本。href<a>

于 2010-09-07T09:08:44.290 回答