PHP URL函数

PHP urldecode() 函数对给定字符串中的任何 形如 %## 字符串编码进行解码。加号 ('+') 被解码为空格字符。

它是PHP urlencode() 函数的反函数。

语法

urldecode(string) 

参数

string必填。 指定要解码的字符串。

返回值

返回解码后的字符串。

示例:

下面的示例显示了urldecode()函数的用法。

<?php
echo urldecode("https%3A%2F%2Fwww.yxjc123.com")."\n";
echo urldecode("https%3A%2F%2Fwww.yxjc123.com%2Fcompilers.php")."\n";
echo urldecode("https%3A%2F%2Fwww.yxjc123.com%2Fqt%2Fquicktables.php")."\n";
?> 

上述代码的输出将是:

https://www.yxjc123.com
https://www.yxjc123.com/compilers.php
https://www.yxjc123.com/qt/quicktables.php 

示例:

考虑下面的示例,该示例显示了字符串的编码方式和

<?php
$str1 = "https://www.yxjc123.com";
$encoded_str1 = urlencode($str1);

echo "The string is: $str1 \n";
echo "Encoded string: $encoded_str1 \n";

$str2 = "https%3A%2F%2Fwww.yxjc123.com";
$decoded_str2 = urldecode($str2);

echo "\nThe string is: $str2 \n";
echo "Decoded string: $decoded_str2 \n";
?> 

上述代码的输出将是:

The string is: https://www.yxjc123.com
Encoded string: https%3A%2F%2Fwww.yxjc123.com 

The string is: https%3A%2F%2Fwww.yxjc123.com 
Decoded string: https://www.yxjc123.com 

示例:

再考虑一个示例,演示如何从使用此函数编码的字符串。

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

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

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

上述代码的输出将是:

Value for parameter "my" is "apples" 
Value for parameter "are" is "green and red"