Wednesday, May 16, 2012

How to get the main domain from the URL

This post is just a code snippet on how to get the main domain from the URL string. Enjoy and happy coding!!

<?php

$url = "http://subdomain.domain.com/sample/url"; 

$host = parse_url($url, PHP_URL_HOST);
$host = array_reverse(explode('.', $host));
$host = $host[1].'.'.$host[0];

echo "$host";

?>

No comments:

Post a Comment