Home  >  Article  >  Backend Development  >  Summary of url processing functions in php_PHP tutorial

Summary of url processing functions in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:071139browse

There are many url processing functions in PHP, such as: http_build_query, compact, urldecode, urlencode, parse_url, rawurldecode and other functions.

http_build_query

(PHP 5) http_build_query — Generate URL-encoded request string

The code is as follows Copy code
 代码如下 复制代码

$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');echo http_build_query($data); // foo=bar&baz=boom&cow=milk&php=hypertext+processor
?>

$data = array('foo'=>'bar',
'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor');echo http_build_query($data); // foo=bar&baz=boom&cow=milk&php=hypertext+processor ?>


 代码如下 复制代码

$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$result = compact("city", "state", "event");
// array('city'=>'"San Francisco"','state'=>'CA','event' => "SIGGRAPH")
?>
-------------

$qs=compact('province','city','name');
foreach($qs as $key => $value){
    if(!$value){
        unset($qs[$key]);
    }
}
$url='something/search/?'.http_build_query($qs) ;

compact


array compact ( mixed $varname [, mixed $... ] )
(PHP 4, PHP 5) compact — Create an array including variable names and their values ​​

The code is as follows Copy code

$city = "San Francisco";

$state = "CA";
代码如下 复制代码

*/
print_r(parse_url("www.bKjia.c0m")); //解析url并输出返回数组

/*

$event = "SIGGRAPH";

$result = compact("city", "state", "event");

// array('city'=>'"San Francisco"','state'=>'CA','event' => "SIGGRAPH")

?>

-------------

$qs=compact('province','city','name');

foreach($qs as $key => $value){
If(!$value){
          unset($qs[$key]);

}

}
$url='something/search/?'.http_build_query($qs) ;

 代码如下 复制代码

*/

echo rawurldecode('foo%20bar%40baz');      //输出foo bar@baz
/*

string rawurlencode ( string str )

urldecode, urlencode Let’s look at the introduction and examples. parse_url($str url); Convert url to array
The code is as follows Copy code
*/ print_r(parse_url("www.bKjia.c0m")); //Parse the url and output the return array /*
url special format string is restored to a normal string. Syntax: string rawurldecode(string str); Return value: String Function type: Encoding processing Content Description This function decodes a string. Decodes a url's string-specific format into a normal string. For detailed encoding and decoding information and specification documents, please refer to rfc 1738.
The code is as follows Copy code
*/ echo rawurldecode('foo%20bar%40baz'); //Output foo bar@baz /* string rawurlencode ( string str )


Returns a string in which all non-alphanumeric characters except -_. are replaced with a percent sign (%) followed by two hexadecimal digits. This is an encoding described in rfc 1738, and is intended to protect literal characters from being interpreted as special URL delimiters, and to protect the URL format from being garbled by character conversions used by the transport medium (like some mail systems). For example, if you want to include the password in the ftp url:

The code is as follows
 代码如下 复制代码

*/
$str="http://www.bKjia.c0m";       //定义字符串
$result=rawurlencode($str);      //对指定字符串编码
echo $result;

/*
urldecode()
url解码
*/         //输出结果

$str="http%3a%2f%2fwww.bKjia.c0m";
$result=urldecode($str);
echo $result;

/*
urlencode()
url编码

*/

$str="http://www.bKjia.c0m";       //定义字符串
$result=urlencode($str);       //对指定字符串编码
echo $result;         //输出结果

Copy code
*/ $str="http://www.bKjia.c0m"; //Define string

$result=rawurlencode($str); //Encode the specified string

echo $result;/* urldecode() url decoding */ //Output result $str="http%3a%2f%2fwww.bKjia.c0m"; $result=urldecode($str); echo $result; /* urlencode()
url encoding */
$str="http://www.bKjia.c0m"; //Define string $result=urlencode($str); //Encode the specified string echo $result; //Output result http://www.bkjia.com/PHPjc/628667.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628667.htmlTechArticleThere are many url processing functions in php, such as: http_build_query, compact, urldecode, urlencode, parse_url, rawurldecode, etc. functions. http_build_query (PHP 5) http_build_query generates URL-...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn