Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

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.
0
webcertified
Asked:
webcertified
  • 11
  • 2
  • 2
  • +1
2 Solutions
 
ClickCentricCommented:
$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"
0
 
ClickCentricCommented:
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.
0
 
ClickCentricCommented:
oops, ignore the first code..wrong function...use this instead:

$retval = substr($_SERVER['HTTP_HOST'],0,strpos($_SERVER['HTTP_HOST'],"."));
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
webcertifiedAuthor Commented:
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"
0
 
ClickCentricCommented:
Geez...I'm having a problem reading right tonight.  Sorry, misread the question.  I'll post another solution in a few minutes here.
0
 
ClickCentricCommented:
$retval = substr($_SERVER['HTTP_HOST'],0,strrpos($_SERVER['HTTP_HOST'],"."));
$retval = substr($retval,0,strrpos($retval,"."));
0
 
ClickCentricCommented:
Accidentally clicked on submit while typing on that one.
0
 
ClickCentricCommented:
$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



0
 
tolgaongCommented:
$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";
0
 
ClickCentricCommented:
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
0
 
ClickCentricCommented:
And to speed it up, you can change:

$result = preg_split('/[\\.]+/i', $url);

to:

$result = preg_split('/[\\.]+/', $url);

As there's no need to make it case insensitive when it's searching for a '.'
0
 
tolgaongCommented:
$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>";
      }
0
 
RoonaanCommented:
Instead of preg_split, rather use explode:

$parts = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = isset($parts[count($parts)-3]) ? $parts[count($parts)-3] : '';

-r-
0
 
ClickCentricCommented:
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.
0
 
ClickCentricCommented:
Hmm...yes, explode() would be better
0
 
RoonaanCommented:
Also, instead of your for loop, better use the array_reverse() function.

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

-r-
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now