By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,407 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,407 IT Pros & Developers. It's quick & easy.

trim

P: n/a
this may seem so lame to some, but I have exausted my brain trying to figure
this out, and therefore throw myself at the group:

basic breakdown of problem

$CurrentSite = $_SERVER["HTTP_HOST"];
<?php echo $CurrentSite ;?>

I only want the 'domain.com' part to display.. even if the host is
www.domain.com or whatever.domain.com

I just want to display the domain.com, am I right in thinking that regular
expressions would be the solution?

any assistance would save my sanity.

Thanks
Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Joe90 <bo******************@hotmail.com> wrote:
I only want the 'domain.com' part to display.. even if the host is
www.domain.com or whatever.domain.com

I just want to display the domain.com, am I right in thinking that regular
expressions would be the solution?


It's a solution, and propable the simpelest if you know how to compose
one (which is quite trivial so it's best to let you do your own research
to get the last 2 words seperated by a '.' :).

An other solution is to count the '.' and if there are more then 2
to take the substring from the second last '.' (strrpos/substr).

Jul 17 '05 #2

P: n/a

Joe90 wrote:
this may seem so lame to some, but I have exausted my brain trying to figure this out, and therefore throw myself at the group:

basic breakdown of problem

$CurrentSite = $_SERVER["HTTP_HOST"];
<?php echo $CurrentSite ;?>

I only want the 'domain.com' part to display.. even if the host is
www.domain.com or whatever.domain.com

I just want to display the domain.com, am I right in thinking that regular expressions would be the solution?


One way:
<?
$CurrentSite = explode('.',$_SERVER["HTTP_HOST"]);
$i = count($CurrentSite) - 2;
echo $CurrentSite[$i] . '.' . $CurrentSite[$i+1];
?>

Ken

Jul 17 '05 #3

P: n/a
> $CurrentSite = $_SERVER["HTTP_HOST"];
<?php echo $CurrentSite ;?>

I only want the 'domain.com' part to display.. even if the host is
www.domain.com or whatever.domain.com


Yes a regular expression would be the key.

You would most likely put your domain at the end and actually have it
filter everything else. The problem however you will run into when
building your regular expression is when there is domain.co.uk and the
similar.

Mike
Jul 17 '05 #4

P: n/a
^[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$

should work ok even with .co.uk :-)

however how would I extract just the domain part, in the case of
thisisasub.domain.com , www.domain.com or what if it was just domain.com the
script would have to be adaptable to all these scenarios.

"Mike Willbanks" <pe****@gmail.com> wrote in message
news:IU*******************@fe06.lga...
$CurrentSite = $_SERVER["HTTP_HOST"];
<?php echo $CurrentSite ;?>

I only want the 'domain.com' part to display.. even if the host is
www.domain.com or whatever.domain.com


Yes a regular expression would be the key.

You would most likely put your domain at the end and actually have it
filter everything else. The problem however you will run into when
building your regular expression is when there is domain.co.uk and the
similar.

Mike

Jul 17 '05 #5

P: n/a
Joe90 wrote:
^[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$

should work ok even with .co.uk :-)

however how would I extract just the domain part, in the case of
thisisasub.domain.com , www.domain.com or what if it was just domain.com the
script would have to be adaptable to all these scenarios.

"Mike Willbanks" <pe****@gmail.com> wrote in message
news:IU*******************@fe06.lga...

$CurrentSite = $_SERVER["HTTP_HOST"];
<?php echo $CurrentSite ;?>

I only want the 'domain.com' part to display.. even if the host is
www.domain.com or whatever.domain.com

Yes a regular expression would be the key.

You would most likely put your domain at the end and actually have it
filter everything else. The problem however you will run into when
building your regular expression is when there is domain.co.uk and the
similar.

Mike


Okay I'll go out on a limb? Why not just use - $domain =
str_replace('www.', '', $domain);

--
*****************************
Chuck Anderson Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Jul 17 '05 #6

P: n/a

"Chuck Anderson" <we************@seemy.sig> wrote in message
news:IY********************@comcast.com...
Joe90 wrote:
^[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$

should work ok even with .co.uk :-)

however how would I extract just the domain part, in the case of
thisisasub.domain.com , www.domain.com or what if it was just domain.com
the script would have to be adaptable to all these scenarios.

Okay I'll go out on a limb? Why not just use - $domain =
str_replace('www.', '', $domain);

WWW isn't the only domain prefix used, for instance a lot of ISPs have a
members.domain.com alias as well that points to their subscribers personal
web sites. Then there is ftp.domain.com, etc..
Jul 17 '05 #7

P: n/a
That is my concern also DON. I will clarify further :

I wish to only have 'domain.com' in a variable.

that would mean that all subdomains like:

whatever.domain.com
ftp.domain.com
mail.domain.com
www.domain.com
supercalifragilisticexpialidocious.domain.com
domain.com

would all except the last one be stripped of the hostname and my variable
would contain only

DOMAIN.COM

It is proving to be a challenge ;-)

"Don Freeman" <fr******@sonic.net> wrote in message
news:hA***************@typhoon.sonic.net...

WWW isn't the only domain prefix used, for instance a lot of ISPs have a
members.domain.com alias as well that points to their subscribers personal
web sites. Then there is ftp.domain.com, etc..

Jul 17 '05 #8

P: n/a
Joe90 wrote:
^[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$

should work ok even with .co.uk :-)

however how would I extract just the domain part, in the case of
thisisasub.domain.com , www.domain.com or what if it was just domain.com the
script would have to be adaptable to all these scenarios.


If you can't be 100% sure what the intended "base domain" is, then do
what many scripts do and simply make it a configuration option.

e.g. You can use wildcard DNS for domains such as
http://anything.goes.here.dev.basedomain.com, and there's no easy way to
know that "anything.goes.here" is the intended host part and
"dev.basedomain.com" is the base part just by looking at HTTP_HOST or
SERVER_NAME.
Jul 17 '05 #9

P: n/a
there has to be another way.. I am sure you can do it with Expression, but I
just cant master it. The whole script relies on just being able to pull
just the hostname.
"Jason F." <ja************@gmail.com> wrote in message
news:J_*******************@twister.nyc.rr.com...
Joe90 wrote:
^[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$

should work ok even with .co.uk :-)

however how would I extract just the domain part, in the case of
thisisasub.domain.com , www.domain.com or what if it was just domain.com
the script would have to be adaptable to all these scenarios.


If you can't be 100% sure what the intended "base domain" is, then do what
many scripts do and simply make it a configuration option.

e.g. You can use wildcard DNS for domains such as
http://anything.goes.here.dev.basedomain.com, and there's no easy way to
know that "anything.goes.here" is the intended host part and
"dev.basedomain.com" is the base part just by looking at HTTP_HOST or
SERVER_NAME.

Jul 17 '05 #10

P: n/a

"Joe90" <bo******************@hotmail.com> wrote in message
news:d5**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
I wish to only have 'domain.com' in a variable.
that would mean that all subdomains like:
whatever.domain.com
ftp.domain.com
mail.domain.com
www.domain.com
supercalifragilisticexpialidocious.domain.com
domain.com

would all except the last one be stripped of the hostname and my variable
would contain only

DOMAIN.COM


The best I could come up with is to break down your full name (like
my.ftp.do.main.com, in the case "do.main.com" will be the actual domain)
into separate elements using the "." as a delimiter. (see
http://us2.php.net/manual/en/function.explode.php on how to do this)

Then taking the last two elements - domain & com (no sense testing on just
the last as there are no domains named thusly) stringing them together with
the "." separating them. Then test it using the checkdnsrr function
(http://us4.php.net/checkdnsrr) if it passes (won't in this case) then that
is your actual domain, if not add the next (going right to left) element to
the string with another "." as separator (now you would have do.main.com).
Do another checkdnsrr, and so on (though in this case it would pass the
test). This would be placed in a loop limited by the number of elements
created.
-Don Freeman
Jul 17 '05 #11

P: n/a
Don Freeman wrote:
"Joe90" <bo******************@hotmail.com> wrote in message
news:d5**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
I wish to only have 'domain.com' in a variable.
that would mean that all subdomains like:
whatever.domain.com
ftp.domain.com
mail.domain.com
www.domain.com
supercalifragilisticexpialidocious.domain.com
domain.com

would all except the last one be stripped of the hostname and my variable
would contain only

DOMAIN.COM

The best I could come up with is to break down your full name (like
my.ftp.do.main.com, in the case "do.main.com" will be the actual domain)
into separate elements using the "." as a delimiter. (see
http://us2.php.net/manual/en/function.explode.php on how to do this)

Then taking the last two elements - domain & com (no sense testing on just
the last as there are no domains named thusly) stringing them together with
the "." separating them. Then test it using the checkdnsrr function
(http://us4.php.net/checkdnsrr) if it passes (won't in this case) then that
is your actual domain, if not add the next (going right to left) element to
the string with another "." as separator (now you would have do.main.com).
Do another checkdnsrr, and so on (though in this case it would pass the
test). This would be placed in a loop limited by the number of elements
created.
-Don Freeman


Good solution, except checkdnsrr() isn't implemented for Windows' PHP,
and it won't work for domains without a MX, or for valid subdomain
websites that do take their mail at the base domain. (eg.
www.johnsmith.domain.com could be a valid vhost who gets mail
@domain.com rather than @johnsmith.domain.com)
Jul 17 '05 #12

P: n/a
how ironic $SERVER_NAME worked..

"Joe90" <bo******************@hotmail.com> wrote in message
news:d5**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
there has to be another way.. I am sure you can do it with Expression, but
I just cant master it. The whole script relies on just being able to pull
just the hostname.
"Jason F." <ja************@gmail.com> wrote in message
news:J_*******************@twister.nyc.rr.com...
Joe90 wrote:
^[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$

should work ok even with .co.uk :-)

however how would I extract just the domain part, in the case of
thisisasub.domain.com , www.domain.com or what if it was just domain.com
the script would have to be adaptable to all these scenarios.


If you can't be 100% sure what the intended "base domain" is, then do
what many scripts do and simply make it a configuration option.

e.g. You can use wildcard DNS for domains such as
http://anything.goes.here.dev.basedomain.com, and there's no easy way to
know that "anything.goes.here" is the intended host part and
"dev.basedomain.com" is the base part just by looking at HTTP_HOST or
SERVER_NAME.


Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.