PHP Url Encoding/Decoding

PHP URL Encoding / Decoding

The weird characters in the values passed in the URL should be escaped, using urlencode().


For example, the following portion of code :

echo urlencode('dsf13f3343f23/23=');

would give you :

dsf13f3343f23%2F23%3D

Which works fine, as an URL parameter.


And if you want to build aquery string with several parameters, take a look at the http_build_query() function.

For example :

echo http_build_query(array(
'id' => 'dsf13f3343f23/23=',
'a' => 'plop',
'b' => '$^@test',
));

will give you :

id=dsf13f3343f23%2F23%3D&a=plop&b=%24%5E%40test

This function deals with escaping and concatenating the parameters itself ;-)

How to encode URL in JS and Decode in PHP?

Try this in your javascript code

window.location.href = 'products.php?price_range='+encodeURIComponent('-INFto2000,2001to5000');

You can access the decoded value in $_GET['price_range']. $_GET variables are decoded by default in PHP.

How to properly URL encode a string in PHP?

For the URI query use urlencode/urldecode; for anything else use rawurlencode/rawurldecode.

The difference between urlencode and rawurlencode is that

  • urlencode encodes according to application/x-www-form-urlencoded (space is encoded with +) while
  • rawurlencode encodes according to the plain Percent-Encoding (space is encoded with %20).

PHP URL decode GET

So here is the problem, the pound sign (#) (Hash) wasn't encoded... since I can't go back and re-encode it I have to use javascript (ex. alert(window.location.hash);) to send me the full URL after the hash then I append it to PHP's version of the URL, I THEN use a find and replace function in PHP to replace the "#" with "%23", then I use the urldecode method and it returns the full proper url decoded.

Converting unfamiliar url encoding

Use urldecode($thing). It does work.

urldecode("https%3A%2F%2Fs3.amazonaws.com%2Fdocs.rightsignature.com%2Fassets..."); 
// https://s3.amazonaws.com/docs.rightsignature.com/assets...

Does PHP automatically do urldecode() on $_POST?

Yes, all the parameters you access via $_GET and $_POST are decoded.

The reason the urldecode() documentation doesn't mention $_POST is because the POST data might not be URL-encoded in the first place. It depends on whether the POST data is submitted in application/x-www-form-urlencoded format or multipart/form-data format.

But all this is transparent to the application.

The documentation of $_GET does mention this explicitly, though.

Note:

The GET variables are passed through urldecode().

URL Decoding in PHP

Your string is also UTF-8 encoded. This will work:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));

Output: "Antônio Carlos Jobim".



Related Topics



Leave a reply



Submit