具有Unicode字符(\ u2022)的PHP字符串不会作为项目符号回显 [英] PHP string with unicode character (\u2022) won't echo as bullet

查看:61
本文介绍了具有Unicode字符(\ u2022)的PHP字符串不会作为项目符号回显的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中包含项目符号•采用以下格式:

I have a string which contains bullets • in the following format:

$string = "Yay bullets!!\n\\u2022\n\\u2022\n\\u2022\n\\u2022\n\\u2022\n#somany";

我需要回显显示项目符号的字符串.我要打印出来的最接近的方法是使 $ string ="\ u2022"; ,然后回显 json_decode(''.$ string.'"'); 可以很好地打印项目符号.

I need to echo the string displaying the bullets. The closest I've come to printing this out is by making $string = "\u2022"; and then echoing json_decode('"' . $string . '"'); which prints the bullet just fine.

当我尝试对完整的 $ string 执行相同操作时,我什么也没有得到,也没有错误.

When I try to do the same to the full $string I get no result at all and no error.

以下是我尝试使用其结果输出的一些内容:

Here are some things I have tried with their resulting output:

$raw = htmlspecialchars("Yay bullets!!\n\\u2022\n\\u2022\n\\u2022\n\\u2022\n\\u2022\n#yes", ENT_QUOTES);

echo $raw;

结果:

Yay bullets!!
\u2022
\u2022
\u2022
\u2022
\u2022
#yes

当我尝试:

echo mb_convert_encoding($raw, 'UTF-8', 'HTML-ENTITIES');

结果:与上述相同.没变化.

Result: Same as above. No change.

当我尝试:

echo json_decode('"' . $raw . '"');

结果:无输出.

当我尝试时:

echo mb_convert_encoding($raw, 'UTF-8', 'UTF-16BE');

结果:奡礠扵汬整猡ℊ屵㈰㈲ੜ甲〲㈊屵㈰㈲ੜ甲〲㈊屵㈰㈲ਣ祥

Result: 奡礠扵汬整猡ℊ屵㈰㈲ੜ甲〲㈊屵㈰㈲ੜ甲〲㈊屵㈰㈲ਣ祥

我非常感谢您帮助我们弄清楚如何正确输出此结果.

I would really appreciate help figuring out how to output this properly.

谢谢.

编辑

除了项目符号外,我还无法打印表情符号参考代码,例如 \\ ud83d \\ udc93 .在这里参考: http://emojipedia.org/face-with-tears-of-欢乐/

In addition to bullets, I am also failing to print emoji reference codes such as \\ud83d\\udc93. Reference here: http://emojipedia.org/face-with-tears-of-joy/

推荐答案

您可以这样做.它为两个工作.请检查

you can do like this. It working for both. please check

 function decodeEmoticons($src) {
    $replaced = preg_replace("/\\\\u([0-9A-F]{1,4})/i", "&#x$1;", $src);
    $result = mb_convert_encoding($replaced, "UTF-16", "HTML-ENTITIES");
    $result = mb_convert_encoding($result, 'utf-8', 'utf-16');
    return $result;
}
$r = "Yay bullets!!\n\\u2022\n\\u2022\n\\u2022\n\\u2022\n\\u2022\n#somany";
echo decodeEmoticons($r);
$src = "\u263a\ud83d\ude00\ud83d\ude01\ud83d\ude02\ud83d\ude03";
echo decodeEmoticons($src);

这篇关于具有Unicode字符(\ u2022)的PHP字符串不会作为项目符号回显的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆