How to parse parse_url parameters into array parse_str
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:
<?php $url='http://www.phpernote.com/ad.php?id=325&action=index&page=3'; $urlarr=parse_url($url); parse_str($urlarr['query'],$parr); print_r($urlarr); print_r($parr);
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

C++ parameter type safety checking ensures that functions only accept values of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

Reference parameters in C++ functions (essentially variable aliases, modifying the reference modifies the original variable) and pointer parameters (storing the memory address of the original variable, modifying the variable by dereferencing the pointer) have different usages when passing and modifying variables. Reference parameters are often used to modify original variables (especially large structures) to avoid copy overhead when passed to constructors or assignment operators. Pointer parameters are used to flexibly point to memory locations, implement dynamic data structures, or pass null pointers to represent optional parameters.
