Android 与 PHP 脚本通信时出现撇号的奇怪问题
我正在 Android 应用程序中与 PHP Web 服务进行通信。所有从 android 发送的请求都使用 UTF-8 编码,并且 php 脚本使用 utf-8 对其进行解码。
但是,当任何请求使用撇号 ' 发送时,php 的解码功能似乎无法按应有的方式工作。
例如,如果我以“今天的星座”形式发送请求,那么它的 utf-8 编码将是“今天%27s+星座”。我尝试用 Android 来解码它,并且成功了。但在 php 中,解码后它给出了相同的文本。
数据库是MySql。这是数据库的问题还是php的问题?我不确定,但是这个问题有解决方法吗?
问候
苏尼尔
I am communicating with PHP web services in Android application. All the requests send from android are encoded with UTF-8 and the php scripts decodes it with utf-8.
But when any request is send with apostrophe ' the decode function of php doesn't seem to work the way it should.
For example, if I send the request as "Today's Horoscope" then its utf-8 encode will be "Today%27s+Horoscope". I tried to decode this with Android and it was successful. But in php it gives the same text after decoding.
The database is MySql. Is this a problem with database or php? I am not sure about it but is there any workaround to this problem?
Regards
Sunil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是 UTF-8 编码,它称为 URL 或百分比编码。在将数据插入数据库之前,尝试通过
urldecode()
运行数据。This is not UTF-8 encoding, it's called URL or percent encoding. Try running the data through
urldecode()
before inserting it into the data base.正如 Pekka 指出的那样,在这种情况下 urldecode 就足够了,这主要是(过度的)预防措施,因为我已经看到 urldecode 失败了
as Pekka pointed out
urldecode
is sufficient in this case, this is mainly (overdone) precaution since I've seenurldecode
failing我在 php 代码中尝试了您的示例,但无法收到您提到的错误。
如果您想在插入数据库之前转义引号,请使用
mysql_real_escape_string($str);
I tried your example in the php code and couldn't get the error as you mentioned.
If you would like to escape the quotes before inserting to the database use
mysql_real_escape_string($str);