In this quick tutorial, I am going to show you How to Quickly Extract Domain Name & it’s components from URL in PHP, In some cases of development you need to extract hostname and it’s parameter individually, For parsing domain name you can simply use php parse_url function to extract domain name, It’ll not only return domain but also Parse the whole URL and return its components in associative array format.
See example below
$url = 'http://www.iamrohit.in/rohit-kumar/?id=1&name=Rohit'; var_dump(parse_url($url)); |
OutPut:
array(4) { ["scheme"]=> string(4) "http" ["host"]=> string(15) "www.iamrohit.in" ["path"]=> string(13) "/rohit-kumar/" ["query"]=> string(15) "id=1&name=Rohit" } |
Extract Domain Name
$url = 'http://www.iamrohit.in/rohit-kumar/?id=1&name=Rohit'; $result = parse_url($url); echo $result['host']; // output: www.iamrohit.in |