Home >Backend Development >PHP Problem >How to convert URL to array in php
In PHP, we sometimes need to convert a URL into a useful array so that we can get information about the page from it. In this article, we will learn how to convert URL to array using PHP functions.
Converting a URL to an array requires using the parse_url() function and parse_str() function in PHP. The Parse_url() function breaks the URL into parts, and the parse_str() function breaks the query string and converts it into an associative array.
First, let’s look at the syntax of the parse_url() function:
parse_url($url, $component);
In this syntax, the $url parameter is the URL to be decomposed, and the $component parameter is the URL component to be returned. . The $component parameter is optional, it can be any of the following:
If the $component parameter is not specified, the parse_url() function will return an associative array containing all parts of the URL.
Here is a sample code that demonstrates how to use the parse_url() function to break a URL into its component parts:
$url = "http://www.example.com/index.php?id=1&name=john#about"; $url_parts = parse_url($url); echo "<pre class="brush:php;toolbar:false">"; print_r($url_parts); echo "";
The above code will produce the following output:
Array ( [scheme] => http [host] => www.example.com [path] => /index.php [query] => id=1&name=john [fragment] => about )
Next , we use the parse_str() function to convert the query string into an associative array. The syntax of the parse_str() function is as follows:
parse_str($query, $result);
In this syntax, the $query parameter is the query string to be decomposed, and the $result parameter is the associative array to save the converted query string.
The following is a sample code that demonstrates how to use the parse_str() function to convert a query string into an associative array:
$url = "http://www.example.com/index.php?id=1&name=john#about"; $url_parts = parse_url($url); parse_str($url_parts['query'], $query); echo "<pre class="brush:php;toolbar:false">"; print_r($query); echo "";
The above code will produce the following output:
Array ( [id] => 1 [name] => john )
Finally , we will use the parse_url() function and parse_str() function to convert the URL into an associative array containing all the components of the URL and the query string.
The following is a sample code to convert a URL to an array:
$url = "http://www.example.com/index.php?id=1&name=john#about"; $url_parts = parse_url($url); parse_str($url_parts['query'], $query); $result = array( 'scheme' => $url_parts['scheme'], 'host' => $url_parts['host'], 'port' => $url_parts['port'], 'user' => $url_parts['user'], 'pass' => $url_parts['pass'], 'path' => $url_parts['path'], 'query' => $url_parts['query'], 'fragment' => $url_parts['fragment'], 'query_array' => $query ); echo "<pre class="brush:php;toolbar:false">"; print_r($result); echo "";
The above code will produce the following output:
Array ( [scheme] => http [host] => www.example.com [port] => [user] => [pass] => [path] => /index.php [query] => id=1&name=john [fragment] => about [query_array] => Array ( [id] => 1 [name] => john ) )
Now, we have successfully converted a URL to an array containing An associative array of all components of the URL and the query string the!
Summary:
In PHP, URLs can be easily converted into arrays using the parse_url() function and parse_str() function. This method is very useful as it allows us to extract the information we need from the URL. We can use the converted array to perform various tasks such as generating sitemaps or extracting useful SEO information.
The above is the detailed content of How to convert URL to array in php. For more information, please follow other related articles on the PHP Chinese website!