Home >Backend Development >PHP Tutorial >Summary of methods for getting url parameters in php, getting url parameters in php_PHP tutorial

Summary of methods for getting url parameters in php, getting url parameters in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:13861browse

Summary of how to get url parameters in php, get url parameters in php

The example in this article describes the method of obtaining url parameters in PHP. Share it with everyone for your reference. The details are as follows:

There are many ways to get the parameters in the url in php. The simplest one is to directly use the parse_url function. It can automatically parse the url parameters and values ​​​​conveniently and quickly and save them in the corresponding array. Others One method is basically based on regular expressions.

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, only to break it 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 _.

Examples are as follows:

Copy code The code is as follows:
$url = "http://www.bkjia.com/welcome/";
$parts = parse_url($url);
print_r($parts);

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


You can also write an algorithm yourself! As follows
Copy code The code is as follows:
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;
}

Call method
Copy code The code is as follows:
$arr = getParams();
print_r($arr);

The running results are as follows:

Copy code The code is as follows:
Array ( [_p] => index [_a] => show [x] => 12 [ y] => 23 )

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/910584.htmlTechArticleA summary of how to get url parameters in php, how to get url parameters in php. This article describes how to get url parameters in php. Share it with everyone for your reference. The details are as follows: How to get the parameters in the url in php...
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