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 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题