Php parses the url (parse_url) parameter into an array (parse_str) _ PHP Tutorial

Source: Internet
Author: User
Php parses the url (parse_url) parameter into an array (parse_str ). The current url is obtained through php. what should I do if I need to extract parameters in the url? This process is actually quite simple. you can use two built-in functions of php to complete the process smoothly. that is, par obtains the current url through php. what should I do if we need to extract parameters in the url? This process is actually quite simple. Using the two built-in functions of php can be completed smoothly, namely the parse_url and parse_str functions. The following describes the two functions and uses examples to describe how to extract parameters in a url.

(1) parse_url (PHP 4, PHP 5)-parse the URL and return its components. the Function prototype is as follows:

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

This function parses a URL and returns an associated array, which contains various components in the URL.

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

Parameter description

The url to be parsed. Invalid characters are replaced.

Component specifies one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY, or PHP_URL_FRAGMENT to obtain the string of the specified part in the URL. (Besides PHP_URL_PORT, an integer value is returned ).

Return value

For URLs that are seriously unqualified, parse_url () may return FALSE.

If the component parameter is omitted, an associated array is returned. At present, at least one element is included in the array. Possible keys in the array include:

Scheme-such as http
Host
Port
User
Pass
Path
Query-question mark? After
Fragment-after the hash symbol #

If the component parameter is specified, parse_url () returns a string (or an integer if PHP_URL_PORT is specified) instead of an array. If the specified component in the URL does not exist, NULL is returned.

(2) parse_str-parses the query string to the variable. the Function prototype is as follows:

Parse_str (string, array)

Parameter description
 
String is required. Specifies the string to be parsed.

Array is optional. Specifies the name of the array that stores the variable. This parameter indicates that the variable is stored in the array. If the array parameter is not set, the variable set by this function overwrites the variable with the same name.

Note: The magic_quotes_gpc setting in php. ini affects the output of this function. If enabled, the variable will be converted by addslashes () before parse_str () resolution.

(3) example of extracting parameters in a url:

 

The output is as follows:

Array
(
[Scheme] => http
[Host] => www.phpernote.com
[Path] =>/ad. php
[Query] ==> id = 325 & action = index & page = 3
)
Array
(
[Id] = & gt; 325
[Action] => index
[Page] => 3
)

Articles you may be interested in
  • In php, we use the curl post method to submit data and the get method to obtain webpage data.
  • URL encoding functions such as urlencode (), urldecode (), rawurlencode (), and rawurldecode ()
  • PHP string escape functions (addslashes, stripslashes)
  • PHP retrieves the complete url function of the current page, including parameters
  • PHP Curl batch multi-threaded open url class
  • Php idea and implementation method for generating short URLs
  • PHP compresses html webpage code to reduce the amount of network data transmitted, clear spaces, tabs, and comment mark
  • PHP uses Curl Functions to Capture webpages and download files with multiple threads

Why? This process is actually quite simple. Using the two built-in functions of php can be completed smoothly, that is, par...

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.