views:

50

answers:

3

Hi,

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

+2  A: 

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
In PHP we have used the same thing i.e urldecode() but still the problem persists. Can you let me know what is the problem?
sunil
@sunil please show the code you are using to insert the data.
Pekka
That's the problem I don't have access to the PHP code. Can you let me know whether its decoding issue or inserting into MySQl?
sunil
@sunil I'm afraid that is impossible to tell without seeing the code.
Pekka
Can you provide me a sample where you have used urldecoding and inserting into mysql? It will be of great help if you can.
sunil
@sunil a simple `$string = $_POST["string"]; $string = urldecode($string);` ... and then insert into the database using the DB of your choice. If you're using old style mySQL functions instead of PDO, don't forget ` $string = mysql_real_escape_string($string);`
Pekka
+1  A: 
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

DrColossos
Is the `rawurldecode` really necessary though? Doesn't `urlencode()` handle both things?
Pekka
You're right but I've seen cases where only both gave the proper solution, no idea why but in this case, `urldecode()` is enough. I just always use both to make sure...
DrColossos
if you have a % sign in text double decoding will break your text.
Naktibalda
A: 

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);

Webrsk