简体   繁体   English

如何在javascript或jquery中编码字符串并使用php对其进行解码

[英]how to encode a string in javascript or jquery and decode it using php

I need to update a field into database using Ajax and PHP. 我需要使用Ajax和PHP将字段更新为数据库。

It works fine but when there are special characters like following 它可以正常工作,但是当有如下特殊字符时

اللهم اني اشكو اليك ضعف قوتي وقلة حيلتي وهواني علي الناس اللهمانياشكواليكضعفقوتيوقلةحيلتيوهوانيعليالناس

The script inserts junk data into the database. 该脚本将垃圾数据插入数据库。

I tried using escape() of javascript to do URL encoding, it encodes the data but when I decode it using PHP it again shows junk values. 我尝试使用javascript的escape()进行URL编码,它对数据进行编码,但是当我使用PHP对其进行解码时,它再次显示垃圾值。

You can try using encodeURIComponent instead of escape , that will give you something like this: 您可以尝试使用encodeURIComponent而不是escape ,这将为您提供以下信息:

%D8%A7%D9%84%D9%84...

whereas escape would yield: escape将产生:

%u0627%u0644%u0644%u0647...

and the unicode escapes might be confusing someone. Unicode换码符可能会使人感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM