urldecode

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

urldecode β€” Π”Π΅ΠΊΠΎΠ΄ΠΈΡ€ΡƒΠ΅Ρ‚ строку, Π·Π°ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½Π½ΡƒΡŽ для бСзопасного Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ Π² структуру URL-адрСса

ОписаниС

function urldecode(string $string): string

Ѐункция Π΄Π΅ΠΊΠΎΠ΄ΠΈΡ€ΡƒΠ΅Ρ‚ Π² строкС ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ %##, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ сформировали Ρ‡Π΅Ρ€Π΅Π· ΠΌΠ΅Ρ…Π°Π½ΠΈΠ·ΠΌ ΠΏΡ€ΠΎΡ†Π΅Π½Ρ‚Π½ΠΎΠ³ΠΎ кодирования. Π‘ΠΈΠΌΠ²ΠΎΠ» '+' дСкодируСтся Π² символ ΠΏΡ€ΠΎΠ±Π΅Π»Π°.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

string

Π‘Ρ‚Ρ€ΠΎΠΊΠ°, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ трСбуСтся Π΄Π΅ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π΄Π΅ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½Π½ΡƒΡŽ строку.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ дСкодирования строки Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ urldecode()

<?php

$query
= "my=apples&are=green+and+red";

foreach (
explode('&', $query) as $chunk) {
$param = explode("=", $chunk);

if (
$param) {
printf(
"Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° \"%s\" β€” \"%s\"<br/>\n",
urldecode($param[0]),
urldecode($param[1])
);
}
}

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅

ΠŸΡ€ΠΈΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ urldecode() ΠΊ элСмСнтам ΡΡƒΠΏΠ΅Ρ€Π³Π»ΠΎΠ±Π°Π»ΡŒΠ½Ρ‹Ρ… массивов $_GET ΠΈ $_REQUEST Π΄Π°Ρ‘Ρ‚ Π½Π΅ΠΎΠΆΠΈΠ΄Π°Π½Π½Ρ‹ΠΉ ΠΈ опасный Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ PHP ΠΈΡ‚Π°ΠΊ Π΄Π΅ΠΊΠΎΠ΄ΠΈΡ€ΡƒΠ΅Ρ‚ значСния ΠΏΠ΅Ρ€Π΅Π΄ записью элСмСнтов Π² массивы $_GET ΠΈ $_REQUEST.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • urlencode() - ΠšΠΎΠ΄ΠΈΡ€ΡƒΠ΅Ρ‚ строку для бСзопасного Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ Π² структуру URL-адрСса
  • rawurlencode() - URL-ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ строки согласно RFC 3986
  • rawurldecode() - Π”Π΅ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ URL-ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠΉ строки
  • » Бтандарт RFC 3986
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
35
alejandro at devenet dot net ΒΆ
15 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

<?php
$x = $_GET['myvar'];
?>

$x store: in the first case "οΏ½" (bad) and in the second case "ΒΊ" (good)

To fix that, you can use this function:

<?php
function to_utf8( $string ) {
// From http://w3.org/International/questions/qa-forms-utf-8.html
    if ( preg_match('%^(?:
      [\x09\x0A\x0D\x20-\x7E]            # ASCII
    | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
    | \xE0[\xA0-\xBF][\x80-\xBF]         # excluding overlongs
    | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
    | \xED[\x80-\x9F][\x80-\xBF]         # excluding surrogates
    | \xF0[\x90-\xBF][\x80-\xBF]{2}      # planes 1-3
    | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
    | \xF4[\x80-\x8F][\x80-\xBF]{2}      # plane 16
)*$%xs', $string) ) {
        return $string;
    } else {
        return iconv( 'CP1252', 'UTF-8', $string);
    }
}
?>

and assign in this way:

<?php
$x = to_utf8( $_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
up
14
Visual ΒΆ
20 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.
up
5
tomas at penajaca dot com dot br ΒΆ
22 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 ("%",$string);

  if (is_array($array)){
    while (list ($k,$v) = each ($array)){
       $ascii = base_convert ($v,16,10);
       $ret .= chr ($ascii);
    }
 }
 return ("$ret");
}
up
2
Joe ΒΆ
18 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.

<?php
echo stripslashes(nl2br($_POST['message']));
?>

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')
up
0
bloodjazman at gmail dot com ΒΆ
6 years ago
"+" replaced by space according to HTML x-www-form-url-encoded media type 
see http://www.faqs.org/rfcs/rfc1866.html