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.
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.
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,strpo s($_SERVER ['HTTP_HOS T'],"."));
$retval = substr($_SERVER['HTTP_HOST
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"
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,strrp os($_SERVE R['HTTP_HO ST'],".")) ;
$retval = substr($retval,0,strrpos($ retval,"." ));
$retval = substr($retval,0,strrpos($
Accidentally clicked on submit while typing on that one.
$test = $_SERVER['HTTP_HOST'];
$retval = substr($test,0,strrpos($te st,"."));
$retval = substr($retval,0,strrpos($ retval,"." ));
$retval = substr($retval,strpos($ret val,".") + 1);
$retval should equal mysub
$retval = substr($test,0,strrpos($te
$retval = substr($retval,0,strrpos($
$retval = substr($retval,strpos($ret
$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";
$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
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$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,$re sult[$i]);
}
echo"<pre>";
print_r($subdomains);
echo"</pre>";
}
//$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,$re
}
echo"<pre>";
print_r($subdomains);
echo"</pre>";
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
$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-
$domain = array_reverse(explode('.',
-r-
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"