Php parses the url and obtains the parameters in the url and obtains the url parameters in four ways _ PHP-php Tutorial

Source: Internet
Author: User
This article describes how to parse a url in php, obtain url parameters, and obtain url parameters in four ways. it involves converting string parameters into arrays and converting parameters into strings, the code in this article is easy to understand. if you are interested, let's take a look at the following code: php parses the url and obtains the parameters in the url. the code is as follows:

<? Php $ url =' http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&area=0&author=0&h=0&region=0&s=1&page=1 '; $ Arr = parse_url ($ url); var_dump ($ arr); $ arr_query = convertUrlQuery ($ arr ['query']); var_dump ($ arr_query ); var_dump (getUrlQuery ($ arr_query);/*** convert the string parameter to an array * @ param $ query * @ return array (size = 10) 'M' => string 'content' (length = 7) 'C' => string 'Index' (length = 5) 'A' => string 'lists' (length = 5) 'catid' => string '6' (length = 1) 'area '=> string '0' (length = 1) 'author' => string '0' (length = 1) 'H' => string '0' (length = 1) 'region' => string '0' (length = 1)'s '=> string '1' (length = 1) 'page' => string '1' (length = 1) */function convertUrlQuery ($ query) {$ queryParts = explode ('&', $ query); $ params = array (); foreach ($ queryParts as $ param) {$ item = explode ('= ', $ param); $ params [$ item [0] = $ item [1];} return $ params ;} /*** change the parameter to a string * @ param $ array_query * @ return string'm = content & c = index & a = lists & catid = 6 & area = 0 & author = 0 & h = 0 & region = 0 & s = 1 & page = 1' (length = 73) */function getUrlQuery ($ array_query) {$ tmp = array (); foreach ($ array_query as $ k => $ param) {$ tmp [] = $ k. '= '. $ param;} $ params = implode ('&', $ tmp); return $ params ;}

The following describes how to obtain php url parameters through four examples.

When URL parameters are known, we can use $ _ GET to obtain the relevant parameter information ($ _ GET ['name']) According to our own situation, how can I obtain the parameter information on the URL in an unknown situation?

First, use the $ _ SERVER built-in array variable

Obtain the URL parameter from the original $ _ SERVER ['query _ string']. Generally, this variable returns data similar to this: name = tank & sex = 1.
To include a file name, you can use $ _ SERVER ["REQUEST_URI"] (The Returned result is similar to:/index. php? Name = tank & sex = 1)

Second, use pathinfo built-in functions

The code is as follows:

<?php$test = pathinfo("http://localhost/index.php");print_r($test);/*

The result is as follows:

Array ([dirname] => http: // localhost // url path [basename] => index. php // Complete file name [extension] => php // file name suffix [filename] => index // file name) */?>

Third, use the parse_url built-in function

The code is as follows:

<?php$test = parse_url("http://localhost/index.php?name=tank&sex=1#top");print_r($test);/*

The result is as follows:

Array ([scheme] => http // protocol used [host] => localhost // host name [path] =>/index. php // path [query] => name = tank & sex = 1 // The parameter [fragment] => top // The anchor behind the root) */?>

Fourth, use the built-in functions of basename

The code is as follows:

<?php$test = basename("http://localhost/index.php?name=tank&sex=1#top");echo $test;/*

The result is as follows:

index.php?name=tank&sex=1#top*/?>

In addition, you can obtain the required value through regular expression matching. This method is more accurate, and the efficiency is not considered for the moment...
Below we will expand the regular expression processing method in practice:

The code is as follows:

<?phppreg_match_all("/(\w+=\w+)(#\w+)?/i","http://localhost/index.php?name=tank&sex=1#top",$match);print_r($match);/*

The result is as follows:

Array(  [0] => Array    (      [0] => name=tank      [1] => sex=1#top    )  [1] => Array     (      [0] => name=tank       [1] => sex=1     )   [2] => Array    (       [0] =>      [1] => #top    ))*/?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.