Home  >  Article  >  Backend Development  >  PHP Get URL Parameter Program Code Summary_PHP Tutorial

PHP Get URL Parameter Program Code Summary_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:17:01779browse

parse_url function

Let’s first understand the parse_url function, the official solution

Description

 mixed parse_url ( string $url [, int $component = -1 ] )

This function parses a URL and returns an associative array containing the various components that appear in the URL.

This function is not used to verify the validity of the given URL, but to break it down into the parts listed below. Incomplete URLs are also accepted and parse_url() will try to parse them as correctly as possible.

The URL to parse. Invalid characters will be replaced with _.

Example

The code is as follows

$url = "http://www.45it.net/welcome/";

代码如下  

$url = "http://www.45it.net/welcome/";

$parts = parse_url($url);

print_r($parts);


array
(
[scheme] => http
[host] => www.45it.net
[path] => /welcome/
)

$parts = parse_url($url);

print_r($parts);


array
(
[scheme] => http
[host] => www.45it.net
[path] => /welcome/
)

  也可以自己去写一个算法!如下

代码如下  


function getParams()
{
$url = '/index.php?_p=index&_a=show&x=12&y=23';

$refer_url = parse_url($url);

$params = $refer_url['query'];

$arr = array();
if(!empty($params))
{
$paramsArr = explode('&',$params);

foreach($paramsArr as $k=>$v)
{
$a = explode('=',$v);
$arr[$a[0]] = $a[1];
}
}
return $arr;
}

代码如下  


function getParams()
{
$url = '/index.php?_p=index&_a=show&x=12&y=23';

$refer_url = parse_url($url);

$params = $refer_url['query'];

$arr = array();
if(!empty($params))
{
$paramsArr = explode('&',$params);

foreach($paramsArr as $k=>$v)
{
$a = explode('=',$v);
$arr[$a[0]] = $a[1];
}
}
return $arr;
}

  调用方法

代码如下  

$arr = getParams();
print_r($arr);

代码如下  

$arr = getParams();
print_r($arr);

  结果

  结果: Array ( [_p] => index [_a] => show [x] => 12 [y] => 23 )

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/372233.htmlTechArticleparse_url函数 我们先来了解一下parse_url函数,官方解决 说明 mixed parse_url ( string $url [, int $component = -1 ] ) 本函数解析一个 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