urlencode vs. rawurlencode

The difference being the asd%20asd vs asd+asd

urlencode differs from RFC 1738 by encoding spaces as + instead of %20

–––––––––––––––––––––––––––––-

One practical reason to choose one over the other is if you’re going to use the result in another environment, for example JavaScript.

In PHP urlencode('test 1') returns 'test+1' 
while rawurlencode('test 1') returns 'test%201' as result.

But if you need to „decode“ this in JavaScript using decodeURI() function then decodeURI("test+1") will give you "test+1" while decodeURI("test%201") will give you "test 1" as result.

In other words the space (“ „) encoded by urlencode to plus („+“) in PHP will not be properly decoded by decodeURI in JavaScript.

In such cases the rawurlencode PHP function should be used.

––––––––––––––––––––––––––––––––-

The only difference is in the way spaces are treated:

urlencode – based on legacy implementation converts spaces to +

rawurlencode – based on RFC 1738 translates spaces to %20

So my advice is to use rawurlencode to produce standards compliant RFC 1738 encoded strings and use urldecode to be backward compatible and accomodate anything you may come across to consume.

––––––––––––––––––––––––––––––––––

Явно наистина само енкодването на спейса е разликата, защото като пробваям нещо по-сложно като например:

<?php
echo urlencode('test 1+2-3.4/5'); 
echo PHP_EOL;
echo rawurlencode('test 1+2-3.4/5');

пак разликата е в „+“ и „%20“

https://stackoverflow.com/questions/996139/urlencode-vs-rawurlencode

Вашият коментар

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *