Get domain without www

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Get domain without www

Post by jabbaonthedais »

I'm trying to strip a domain from a long url, not including the www.

So for http://www.whatever.com it would result "whatever.com".
But it also needs to work for other domain extensions, such as .co.uk, etc.

So http://whatever.co.uk would result "whatever.co.uk"


I came up with this so far:

Code: Select all

$domain = parse_url($referer);
// take out the www dot
$trimmed = trim($domain[host], "www.");
echo $trimmed;
But, if my first letter in the domain is a W, it erases it also. Any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A regular expression or strpos() could be of use.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

#http://(?:www\.)?(.*)#
?
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

edit: Ok, this seems to be working fine:

Code: Select all

$domain = parse_url($referer);
// take out the www dot
$string = ereg_replace('www.', '', $domain[host]);
echo $string;
Do you see any negative results down the road with that? I put in quite a few urls and all seem to work.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

PCRE is faster than the ereg functions, and you can be a whole lot safer!

Code: Select all

$domain = preg_replace('#^(?:https?://)?(?:www\.)?(.*?)(?:/.*)?$','$1',$referer);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What's wrong with strpos()? it's even faster still.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Code: Select all

if(stripos($domain['host'], 'www.')) {
    $referer = str_ireplace('www.', '', $referer, 1);
}
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

Ok, this is what I've got now:

Code: Select all

$domain = parse_url($referer);
$newurl = $domain['host'];

// take out the www dot
$found = stripos($newurl, 'www.');
if ($found !== false) {
 $newurl = str_ireplace('www.', '', $newurl);
}
aaronhall, I couldn't ever get that if statment to go off. No clue why. I took the ", 1" out of the end, made the host variable a real string, and still no luck.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

does my code not work? It might be marginally slower, but it's safer...
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

jabbaonthedais wrote:Ok, this is what I've got now:

Code: Select all

$domain = parse_url($referer);
$newurl = $domain['host'];

// take out the www dot
$found = stripos($newurl, 'www.');
if ($found !== false) {
 $newurl = str_ireplace('www.', '', $newurl);
}
There's no reason to use str_replace when position of the subject is exactly known. Just strip first 4 symbols off, that's all:

Code: Select all

$host = "www.xyz.com";

if(stripos($host, "www.") === 0) // note three =
	$host = substr($host, 4);

echo $host;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That doesn't account for http://www.something.com. Why san't you just do a straight str_replace on 'www.'. If it is there, it is removed. If it is not there, it is not removed because it can't be.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Everah wrote:That doesn't account for http://www.something.com.
My code is for hostnames only. For parsing full urls, parse_url() should be used first, as OP showed.
Why san't you just do a straight str_replace on 'www.'. If it is there, it is removed. If it is not there, it is not removed because it can't be.
this won't work for e.g. "mywww.com"
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think I like kieran's the best.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

Kieran Huggins wrote:does my code not work? It might be marginally slower, but it's safer...
It's not working for me when I use it exactly as you put it... Where can I find a reference on those characters you use in searching? Like below:

Code: Select all

#^(?:https?:)?(?:www\.)?(.*?)(?:/.*)?$'
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

http://www.devguru.com/Technologies/ecm ... cters.html

It is primarily for javascript, but actually works pretty well as an explanation of regular expressions. I think d11wtq also wrote a tutorial on regex oin the regular expressions forum.
Post Reply