Strange problem of apostrophe in communicating with PHP script from Android
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 Horos开发者_如何学Ccope" 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
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.
urldecode(rawurldecode("Today%27s+Horoscope"));
as Pekka pointed out urldecode
is sufficient in this case, this is mainly (overdone) precaution since I've seen urldecode
failing
I tried your example in the php code and couldn't get the error as you mentioned.
$str = "Today's Horoscope";
echo $str; //Today's Horoscope
echo "Encoded: ".urlencode($str); //Encoded: Today%27s+Horoscope
echo "Decoded: ".urldecode($str); //Decoded: Today's Horoscope
If you would like to escape the quotes before inserting to the database use
mysql_real_escape_string($str);
精彩评论