NovumCoder
NovumCoder

Reputation: 4637

PHP's rawurlencode is not equal to JavaScripts escape! Why?

i realized when i used urlencode or rawurlencode in PHP encoding the simple character § (paragraph) i get the following result: "%C2%A7".

But when i use escape in Javascript to encode that character, i get only "%A7".

In this case i have encoding problems when sending/receiving data between the server running PHP and the javascript client trying to fetch the data via ajax/jquery.

I want to be able to write any type of text i want. For this i encode the text and send it to the backend php script, escaping the data and sending. When i retrieve it, on php side i take the data from mysql and do rawurlencode and send it back.

Both sides, work in UTF-8 mode. jquery ajax function is called with "contentType: application/x-www-form-urlencoded:charset=UTF-8", mysql server is set for UTF-8 both for client and server, and the php script starts echoing with header( "application/x-www-form-urlencoded:charset=UTF-8");

Why is PHP producing that %C2 thing, which generates the character  on javascript side.

Coult somebody help?

Upvotes: 3

Views: 5872

Answers (2)

Radoslav Georgiev
Radoslav Georgiev

Reputation: 1376

I had the same problem a while ago and found the solution :

function rawurlencode (str) {
    str = (str+'').toString();        
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A');
}

The code is taken from here - http://phpjs.org/functions/rawurlencode:501 Hope it helps.

Upvotes: 3

ThiefMaster
ThiefMaster

Reputation: 318508

It's clearly a charset íssue:

[adrian@cheops3:~]> php -r 'echo rawurlencode(utf8_encode("§"));'
%C2%A7
[adrian@cheops3:~]> php -r 'echo rawurlencode("§");'
%A7

(the terminal is obviously not running in utf8 mode)

If you have a literal § in your PHP code ensure that the php file is saved as UTF8.

Upvotes: 3

Related Questions