duanmeng2842 2019-04-12 01:08
浏览 254
已采纳

将\ u表情符号字符串转换为电子邮件正文的utf-8

I am having the following emoji in a PHP string variable

$emoji = "\u{1F9D1}\u{1F4AC}";
echo $emoji;

This code above will print the following emoji.

  • 写回答

1条回答 默认 最新

  • dqbhdsec59405 2019-04-12 01:14
    关注
    $foo = preg_replace('#\\\u\{(.*?)\}#', '&#x$1;', $emoji);
    
    • \u needs to be escaped because it has special meaning in a regular expression, and since the backslash also has special meaning in PHP text literals, we need three of them here.

    • { and } also have special meaning, so they need to get escaped with a single backslash.

    • (.*?) matches everything (expect newlines), ? makes it ungreedy.

    • I added an ; at the end in the replacement - browsers are fault tolerant when it’s missing, but it is technically required by HTML syntax.


    And the “other direction”, as requested:

    $emojihtml = '🧑💬';
    
    $bar = preg_replace('~&#x(.*?);~', '\u{$1}', $emojihtml);
    

    (I used ~ for the regex delimiters here, because # is part of what we want to match, saves on escaping.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部