dounianxie2058 2018-02-20 13:43
浏览 113
已采纳

JQuery将HTML实体/表情符号发布到PHP服务器

I'm using JQuery to serialize and post form values to a PHP/MySQL server, and the form textarea contains text and emojis as HTML entities:

<textarea>Hello &#x1f600; and goodbye</textarea>

The server receives:

Hello \ud83d\ude00 and goodbye

However, only "Hello" gets stored in the database correctly. The emoji/entity and any text after it disappear. The database gets:

Hello

Everything is UTF-8 throughout.

What's the right way to parse this into something that can be stored and then returned back into HTML in a way that it renders properly? I must be overlooking something simple.

  • 写回答

1条回答 默认 最新

  • doulao3905 2018-02-20 14:49
    关注

    Solved.

    Either change database/table collation, as per Andy Foster in the comments below the question, or JSON encode/decode to ensure it gets saved properly as a string.

    json_encode($str);
    json_decode($str);
    

    In my case, there were three things affecting encoding/rendering:

    1) Initial submit using JQuery serialize() which converted the emoji into a Unicode escape sequence.

    2) Saving the Unicode escape sequence into database. I used JSON encoding to get past this.

    3) Output escaping when rendering the JSON-decoded emoji string back into an HTML template.

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?