Home >Backend Development >PHP Tutorial >How to Extract the Domain from a URL Using PHP's `parse_url()` Function?
How to Parse Domain from a URL in PHP
In PHP, the parse_url() function can be used to extract the domain from a URL. This function returns an associative array containing various components of the URL, including the host key which corresponds to the domain.
Example:
To parse the domain from the following URL:
http://google.com/dhasjkdas/sadsdds/sdda/sdads.html
Use the following code:
$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html'; $parse = parse_url($url); echo $parse['host']; // prints 'google.com'
Note:
The above is the detailed content of How to Extract the Domain from a URL Using PHP's `parse_url()` Function?. For more information, please follow other related articles on the PHP Chinese website!