最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Feb 3, 2011 at 14:54 NovumCoderNovumCoder 4,7079 gold badges46 silver badges60 bronze badges 1
  • Looking at the PHP docs it appears a user named omid had the same trouble. – Brad Christie Commented Feb 3, 2011 at 14:58
Add a ment  | 

2 Answers 2

Reset to default 3

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/functions/rawurlencode:501 Hope it helps.

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.

发布评论

评论列表(0)

  1. 暂无评论