How to parse parse_url parameters into array parse_str

小云云
Release: 2023-03-17 16:48:02
Original
1914 people have browsed it

The current url is obtained through php. What should I do if I need to extract the parameters in the url? This process is actually quite simple and can be completed successfully using two built-in functions of PHP, namely parse_url and parse_str functions. The following will give a brief description of these two functions and use examples to illustrate how to extract the parameters in the 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 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.

Parameter description

url The URL to be parsed. Invalid characters will be replaced with _.

component Specify 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 get the string of the specified part of the URL. (Except when specified as PHP_URL_PORT, an integer value will be returned).

Return value

For severely unqualified URLs, parse_url() may return FALSE.

If the component parameter is omitted, an associative array array will be returned, and at least one element will currently be in the array. The possible keys in the array are:

scheme - such as http
host
port
user
pass
path
query - after the question mark?
fragment - after the hash symbol

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

(2)parse_str - Parse the query string into a variable. The function prototype is as follows:

parse_str(string,array)

Parameter description

string required. Specifies the string to be parsed.

array Optional. Specifies the name of the array to store the variable. This parameter instructs the variable to be stored in an array. If the array parameter is not set, variables set by this function will overwrite variables already set by the same name.

Note: The magic_quotes_gpc setting in php.ini affects the output of this function. If enabled, variables are converted by addslashes() before being parsed by parse_str() .

(3) Specific examples of parameters extracted from the url are as follows:


        
Copy after login

The results are as follows:

Array
(
[scheme] => http
[host] => www.phpernote.com
[path] => /ad.php
[query] => id=325&action=index&page=3
)
Array
(
[id] => 325
[action] => index
[page] => 3
)

The above content is the PHP parsed url (parse_url) Tutorial on how to convert parameters into an array (parse_str). I hope it can help everyone.

Related recommendations:

"Problem of PHP parsing URL encoding"

##Three examples of PHP parsing URL_PHP tutorial

Three examples of php parsing url

The above is the detailed content of How to parse parse_url parameters into array parse_str. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!