Hi, i need help printing the domain of my site like this: $domain = $_SERVER['HTTP_HOST']; PHP: anyone can help me to only print "domain.com" ? Thanks in advance,
HTTP_HOST will print whatever host you're on, so if you're on www.domain.com it'll say www.domain.com. IF you just want to auto strip the www in front off of it (basically allowing subdomains) $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']); PHP: Course I generally use $_SERVER["SERVER_NAME"] bout the same thing though.
thanks alot any idea how to strip also subdomains and .com extension? Basically i only wan't to get the domain name out, but without errors..
Assuming the domain you're grabbing is not a .co.uk or .com.tw etc and just your basic .com .net .org .ca. .us .me .info, you could do this. $darray = explode('.', $_SERVER['HTTP_HOST']); $narray = array_reverse($darray); $domain = $narray[1]; unset($darray, $narray); PHP: What would happen, lets say your domain is www.domain.com explodes it by . thus making $darray an array of [www][domain][com] But since subdomains could be multiple words, and we know with the exception of co.uk and similar TLDs that the domain is second to last we can quickly reverse the array to get [com][domain][www], then we pull the value of the second element (arrays start at zero) instead of having to count the elements then subtract one.
wohah, thanks!! Such a great explanation, i start getting used to PHP.. but it's still a nightmare to me^^