PHP, like Python, provides the function parse_url(string $url, int $component = -1), which is very convenient.
This function takes two parameters:
url: This parameter is the URL to be parsed (in string format).component: Use PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY, PHP_URL_FRAGMENT to specify the output URL part (except PHP_URL_PORT is int type, others are output as string type).component, this parameter defaults to -1, and an array will be output.Give some examples to help understanding. Assume the URL is https://www.example.com/?keyword=abc&id=12. code show as below:
<?php
$url="https://www.example.com/?keyword=abc&id=12";
//Display the formatted array
var_dump(parse_url($url));
//Output the content corresponding to 'query' of the formatted array
echo parse_url($url)['query'];
//Newline
echo '<br>';
//The content specified here is the host part.
echo parse_url($url,PHP_URL_HOST);
?>
The output content is as follows:
array(4) {
["scheme"]=>
string(5) "https"
["host"]=>
string(15) "www.example.com"
["path"]=>
string(1) "/"
["query"]=>
string(17) "keyword=abc&id=12"
}
keyword=abc&id=12
www.example.com
I hope these will help someone in need~