PHP rawurlencode is not equal to JavaScripts output! What for?

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

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

In this case, I have problems with encoding when sending / receiving data between the server running PHP and the javascript client trying to retrieve data through ajax / jquery.

I want to write any text that I want. To do this, I encode the text and send it to the backend php script, avoiding data transfer and sending. When I get it, on the php side, I take the data from mysql and make rawurlencode and send it back.

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

Why does PHP create this% C2 thing that generates a ร‚ character on the javascript side.

Will someone help the cult?

+3
source share
2 answers

I had the same problem a while ago and found a 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/rawurlencodecode01 Hope this helps.

+3
source

This is clearly the encoding:

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

(the terminal obviously does not work in utf8 mode)

If you have literal ยงin your PHP code, make sure the php file is saved as UTF8.

+3
source

Source: https://habr.com/ru/post/1789775/


All Articles