Only Domain Name from URL using Regular Expression

Share this Article :


Dear Friends, as PHP 5.3.x is released and you will be aware of the truth that ereg() regular expression functions are deprecated from the latest versions i.e no more usage of ereg()

So,  keep this  in mind not to use of  ereg()  from today onwards :)    This  effects  most of the  popular  CMS   i,e  Drupal,  Joomla So,  before you upgrade your  PHP version on your server,  know  the  facts  about  weather  your Application   supports  PHP Latest  versions  or not.

Coming to the  Point , Some times  while tracking  user information   we may need to  grab  the  User Domain  name  part  through coding part.  Here  is  simple  function   which  makes your  life  simpler .

/*  function  to  return only  Domain Name  from the URL  variable
 *  This  function takes  one parameter i.e  URL  variable  and returns  the domain name
  *
/*
function onlyDomain($url)
{
preg_match('@^(?:http://)?([^/]+)@i',$url, $matches);
$host = $matches[1];
 
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
return $matches[0];
}
 
/*  end of the function

Hope this function will help

Written by admin

{admin has written 3699 posts on ITTreats.com . See all posts by }


Leave a Reply