百科狗-知识改变命运!
--

urldecode() - php 网址URL函数

梵高1年前 (2023-11-21)阅读数 12#技术干货
文章标签字符串

urldecode()

(PHP 4, PHP 5, PHP 7)

解码已编码的 URL 字符串

说明

urldecode(string $str) : string

解码给出的已编码字符串中的任何%##。加号('+')被解码成一个空格字符。

参数

  • $str:要解码的字符串。

返回值

返回解码后的字符串。

范例

注释

Warning

urldecode() - php 网址URL函数

超全局变量$_GET和$_REQUEST已经被解码了。对$_GET或$_REQUEST里的元素使用urldecode()将会导致不可预计和危险的结果。

参见

  • urlencode()编码 URL 字符串
  • rawurlencode()按照 RFC 3986 对 URL 进行编码
  • rawurldecode()对已编码的 URL 字符串进行解码
  • » RFC 3986
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 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 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
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.
A reminder: if you are considering using urldecode() on a $_GET variable, DON'T!
Evil PHP:

Good PHP:

The webserver will arrange for $_GET to have been urldecoded once already by the time it reaches you!
Using urldecode() on $_GET can lead to extreme badness, PARTICULARLY when you are assuming "magic quotes" on GET is protecting you against quoting.
Hint: script.php?sterm=%2527 [...]
PHP "receives" this as %27, which your urldecode() will convert to "'" (the singlequote). This may be CATASTROPHIC when injecting into SQL or some PHP functions relying on escaped quotes -- magic quotes rightly cannot detect this and will not protect you!
This "common error" is one of the underlying causes of the Santy.A worm which affects phpBB 
For compatibility of new and old brousers:
%xx -> char
%u0xxxx -> char
function unicode_decode($txt) {
 $txt = ereg_replace('%u0([[:alnum:]]{3})', '\1;',$txt);
 $txt = ereg_replace('%([[:alnum:]]{2})', '\1;',$txt);
 return ($txt);
}
Even if $_GET and $_REQUEST are decoded automatically, some other variables aren't. For example $_SERVER["REQUEST_URI"]. Remember to decode it when using.
mkaganer at gmail dot com:
try using encodeURI() instead of encode() in javascript. That worked for me, while your solution did not on __some__ national characters (at least in IE6).
This function doesn't decode unicode characters. I wrote a function that does.
function unicode_urldecode($url)
{
  preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);
  
  foreach ($a[1] as $uniord)
  {
    $dec = hexdec($uniord);
    $utf = '';
    
    if ($dec 
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')
B.H.
I had troubles converting Unicode-encoded data in $_GET (like this: %u05D8%u05D1%u05E2) which is generated by JavaScript's escape() function to UTF8 for server-side processing.
Finally, i've found a simple solution (only 3 lines of code) that does it (at least in my configuration):

note that documentation for html_entity_decode() states that "Support for multi-byte character sets was added at PHP 5.0.0" so this might not work for PHP 4
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");
}
It seems that the $_REQUEST global parameter is automatically decoded only if the content type is application/x-www-form-urlencoded.
if the content type is multipart/form-data. the data remains un-decoded. and we have to manually handle the decoding at our end
When sending a string via AJAX POST data which contains an ampersand (&), be sure to use encodeURIComponent() on the javascript side and use urldecode() on the php side for whatever variable that was. I've found it tricky to transfer raw ampersands and so this is what worked for me:

For some reason, a variable with an ampersand would stay encoded while other POST variables were automatically decoded. I concatenated data from an html form before submitting, in case you wish to know what happened on the browser end.
nataniel, your function needs to be corrected as follows:
------------------------------------------------------------
function unicode_decode($txt) {
 return ereg_replace('%u([[:alnum:]]{4})', '\1;',$txt);
}
------------------------------------------------------------
since some codes does not begin with %u0.
Send json to PHP via AJAX (POST)
If you send json data via ajax, and encode it with encodeURIComponent in javascript, then on PHP side, you will have to do stripslashes on your $_POST['myVar'].
After this, you can do json_decode on your string.
Ex.: 
If you have a "html reserved word" as variable name (i.e. "reg_var") and you pass it as an argument you will get a wrong url. i.e.
go
you will get a wrong url like this 
"pippo.php?param1=_var"
Simply add a space between "&" and "reg_var" and it will work!
go
"pippo.php?param1=&%20reg_var"
Works!!
About reg_var and "html reserved words"
Do not add spaces as the user suggests.
Instead, do what all HTML standards says and encode & in URLs as & in your HTML. 
The reason why & works "most of the time" is that browsers are forgiving and just decode the & as the &-sign. This breaks whenever you have a variable that matches an HTML entity, like "gt" or "copy" or whatever. &copy in your URL will be interpreted as © (the ; is not mandatory in SGML as it is "implied". In XML it is mandatory.).  The result will be the same as if you had inserted the actual character into your source code, for instance by pressing alt-0169 and actually inserted  in your HTML.
Ie, use:
mylink
Note that the decoding of & to & is done in the browser, and it's done right after splitting the HTML into tags, attributes and content, but it works both for attributes and content.
This mean you should &entitify all &-s in any other HTML attributes as well, such as in a form with 
.
This seems to decode correctly between most browsers and charater coding configurations. Specially indicated for direct parsing of URL as it comes on environment variables:
function crossUrlDecode($source) {
  $decodedStr = '';
  $pos = 0;
  $len = strlen($source);
  while ($pos  127) {
      $decodedStr .= "".ord($charAt).";";
      $pos++;
    }
    elseif($charAt == '%') {
      $pos++;
      $hex2 = substr($source, $pos, 2);
      $dechex = chr(hexdec($hex2));
      if($dechex == '') {
        $pos += 2;
        if(substr($source, $pos, 1) == '%') {
          $pos++;
          $char2a = chr(hexdec(substr($source, $pos, 2)));
          $decodedStr .= htmlentities(utf8_decode($dechex . $char2a),ENT_QUOTES,'ISO-8859-1');
        }
        else {
          $decodedStr .= htmlentities(utf8_decode($dechex));
        }
      }
      else {
        $decodedStr .= $dechex;
      }
      $pos += 2;
    }
    else {
      $decodedStr .= $charAt;
      $pos++;
    }
  }
  return $decodedStr;
}
About: bellani at upgrade4 dot it
$str = "pippo.php?param1=&reg_var";
echo rawurldecode($str);
Gives:
pippo.php?param1=_var
Instead of using a space you should exchange & with the correct W3C &
Like this:
$str = "pippo.php?param1=&reg_var";
echo rawurldecode($str);
To allow urldecode to work with Brazilian characters as and other just place this header command :
header('Content-type: text/html; charset=UTF-8');

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)