Link to home
Start Free TrialLog in
Avatar of webcertified
webcertified

asked on

php trim

Hi I want to get a subdomain name only using "$_SERVER['HTTP_HOST']" and I think the php trim function will be the easiest way to do it. It should only return subdomain name.

For example:

mysub.somedomain.com => mysub
www.mysub.somedomain.com => mysub

Somedomain.com could be anything so it can't use the fixed length count.

Thanks in advance.
Avatar of ClickCentric
ClickCentric

$retval = substr($_SERVER['HTTP_HOST'],0,strstr($_SERVER['HTTP_HOST'],"."));
echo $retval;

trim isn't really any good for this..it could be made to work, but you'd have to go to extremes to do it.  It's more aimed at turning things like "     string     " into "string"
Also, the above will only return the lowest-level subdomain.  So if you have www.mysub.somedomain.com, it'll only return www.  I'm not sure if you need anything different as the question only said the subdomain.
oops, ignore the first code..wrong function...use this instead:

$retval = substr($_SERVER['HTTP_HOST'],0,strpos($_SERVER['HTTP_HOST'],"."));
Avatar of webcertified

ASKER

If  I don't use the trim function what do you recommend to use?
Because I need to return both subdomain name from

"www.mysub.domain.com" returns "mysub"
"mysub.domain.com" returns "mysub"
Geez...I'm having a problem reading right tonight.  Sorry, misread the question.  I'll post another solution in a few minutes here.
$retval = substr($_SERVER['HTTP_HOST'],0,strrpos($_SERVER['HTTP_HOST'],"."));
$retval = substr($retval,0,strrpos($retval,"."));
Accidentally clicked on submit while typing on that one.
$test = $_SERVER['HTTP_HOST'];
$retval = substr($test,0,strrpos($test,"."));
$retval = substr($retval,0,strrpos($retval,"."));
$retval = substr($retval,strpos($retval,".") + 1);


$retval should equal mysub



$url=$_SERVER['HTTP_HOST'];
$result = preg_split('/[\\.]+/i', $url);
$arrcount=count($result);
if ($arrcount==3) echo "There is no subdomain";
else if ($arrcount==4) echo $result[1]." is subdomain";
Actually, if you always want to return the 1st level subdomain:

$url=$_SERVER['HTTP_HOST'];
$result = preg_split('/[\\.]+/i', $url);
$arrcount=count($result);
echo $result[$arrcount -3];

This will return mysub even if the domain is www.server.section.department.mysub.domain.com
ASKER CERTIFIED SOLUTION
Avatar of ClickCentric
ClickCentric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$url='www.server.section.department.mysub.domain.com';
//$url=$_SERVER['HTTP_HOST'];
$result = preg_split('/[\\.]+/', $url);
$arrcount=count($result);
if ($arrcount==3) echo "There is no subdomain";
else if ($arrcount>4) {
      $subdomains=array();
      for($i=1;$i<$arrcount-2;$i++){
            array_push($subdomains,$result[$i]);
            }
      echo"<pre>";
      print_r($subdomains);
      echo"</pre>";
      }
SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or:

$url='www.server.section.department.mysub.domain.com';
$result = preg_split('/[\\.]+/', $url);
$arrcount=count($result);
     for($i=$arrcount;$i>0;$i--){
         $subdomains[]=$result[$i];
          }
     echo"<pre>";
     print_r($subdomains);
     echo"</pre>";

This way, you avoid the function call of array_push which adds overhead and the order of the domain sections is reversed, so element 3 is always the first level subdomain and then you can count up from there to get deeper into the subdomains.
Hmm...yes, explode() would be better
Also, instead of your for loop, better use the array_reverse() function.

$domain = array_reverse(explode('.', $_SERVER['HTTP_HOST']));

-r-