ITTreats.com
Enter your Search term and Hit "Enter"

Wed, 10 Mar 2010 9:37:16 AM +00:00


Only Domain Name from URL using Regular Expression

Only Domain Name from URL using Regular Expression
Posted on January 12th, 2010 in PHP by admin

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


P.S. : Please post us Your Valuable Comments on this Article

Written by : admin
( Treats Given : 6 Treats Attended : )

Key Terms


Post a comment