PHP: Hypertext Preprocessor (original) (raw)

urldecode

(PHP 4, PHP 5, PHP 7, PHP 8)

urldecode — Decodes URL-encoded string

Description

Parameters

string

The string to be decoded.

Return Values

Returns the decoded string.

Examples

Example #1 urldecode() example

`<?php
$query = "my=apples&are=green+and+red";

foreach (

explode('&', query)asquery) as query)aschunk) { param=explode("=",param = explode("=", param=explode("=",chunk);

if (

$param) {
printf("Value for parameter "%s" is "%s"
\n", urldecode($param[0]), urldecode($param[1]));
}
}
?>`

Notes

Warning

The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.

See Also

Found A Problem?

alejandro at devenet dot net

14 years ago

`When the client send Get data, utf-8 character encoding have a tiny problem with the urlencode.
Consider the "º" character.
Some clients can send (as example)
foo.php?myvar=%BA
and another clients send
foo.php?myvar=%C2%BA (The "right" url encoding)

in this scenary, you assign the value into variable $x

x=x = x=_GET['myvar']; ?>

$x store: in the first case "�" (bad) and in the second case "º" (good)

To fix that, you can use this function:

and assign in this way:

x=toutf8(x = to_utf8( x=toutf8(_GET['myvar'] ); ?>

$x store: in the first case "º" (good) and in the second case "º" (good)

Solve a lot of i18n problems.

Please fix the auto-urldecode of $_GET var in the next PHP version.

Bye.

Alejandro Salamanca

`

Visual

18 years ago

If you are escaping strings in javascript and want to decode them in PHP with urldecode (or want PHP to decode them automatically when you're putting them in the query string or post request), you should use the javascript function encodeURIComponent() instead of escape(). Then you won't need any of the fancy custom utf_urldecode functions from the previous comments.

tomas at penajaca dot com dot br

21 years ago

`urldecode does not decode "%0" bypassing it. I can cause troble when you are working with fixed lenght strings.

You can you the function below.

function my_urldecode($string){

array=split("array = split ("%",array=split("string);

if (is_array($array)){
while (list ($k,$v) = each ($array)){
ascii=baseconvert(ascii = base_convert (ascii=baseconvert(v,16,10);
ret.=chr(ret .= chr (ret.=chr(ascii);
}
}
return ("$ret");
}

`

Joe

17 years ago

`It's worth pointing out that if you are using AJAX and need to encode strings that are being sent to a PHP application, you may not need to decode them in PHP.

Will properly output a message sent with the javascript code if the message is encoded:

message = encodeURIComponent(message)

And is sent with an AJAX POST request with the header:
ajaxVar.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

`