CancelImage Upload

Replacing %3A, %2F & %3D with slashes, question marks, etc inside URLs in PHP

When passing along a URL as a Get variable, certain symbols need to be converted into a percentage sign, and a two digit hexadecimal equivalent of the corresponding ASCII symbol.

Therefore a colon turns into %3A, slashes are converted into %2F, question marks become %3F and equals becomes %3D. As a result the URL http://www.example.com/index.php?page=260&id=22 would end up looking something like this:

http%3A%2F%2Fwww.example.com%2Findex.php%3Fpage%3D260%26id%3D22

While a URL in this format will do well as a Get variable, it will not be much use to us as a url in it's current format. Clearly some decoding work is needed. And this can be gone about in two ways. Method number one: put together a regular expression or a few lines of str_replace(). Method number two: use the predefined PHP function rawurldecode(), save a lot of time, and go chilax. I know which method I would prefer:

<?php

$raw_url = "http%3A%2F%2Fwww.example.com%2Findex.php%3Fpage%3D260%26id%3D22";

$decoded_url = rawurldecode($raw_url);

echo $decoded_url;

?>



Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com