Enjoy unlimited access to all forum features for FREE! Optional upgrade available for extra perks.

PHP eregi domain

Status
Not open for further replies.
Joined
Sep 28, 2006
Posts
403
Reaction score
6
Any simple way to dig out just the domain without hlq?
www.test.co.uk

Ive also noticed $_SERVER['SERVER_NAME'] sometimes contains WWW. that needs stripping.

blar.domain.co.uk = domain
www.domain.co.uk = domain
domain.co.uk = domain
 
Last edited:
Eregi for detecting domain names is crazy as http://www.i.am.adding.many.sub-domain.test.co.uk is a valid name, so it takes some fancy regex to detect all the varients, I wrote about 100 lines of code to do this and still had some name getting thru so I went with a quick and nasty snippet.

PHP:
/////////////// START DOMAIN CAPTURE BLOCK ////////////////
	$domainz = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1);
	if (strlen($domainz) <= 6) {
		$domainz = $_SERVER['HTTP_HOST'];
		if($debug == true) { echo '<p>var $domainz - Length: ' . strlen($domainz) . ' - Value: ' .  $domainz . ' - Sub Domain: No</p>'; }
	} else {
		$domainz = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1);
		if($debug == true){ echo '<p>var $domainz - Length: ' . strlen($domainz) . ' - Value: ' .  $domainz . ' - Sub Domain: Yes</p>'; }
	}	
	//////////////// END DOMAIN CAPTURE BLOCK /////////////////

This works for most .uk names that I tested, if it was for a client I'd have my programming house write it but this was a parking script after I got spanked over my signature links :p
 
Whizz.... Correct

Skinner.... thanks for the link but:-
My draft looks similar to your snippet but caters and removes sub domain levels. Only failed style of domain would be two char hlq domains (xx.xx.CC or WS style of names) with one char subdomain.
I dont own any so not a problem for my needs.

PHP:
$arr = array("/", "?", "%");
$pos = "256";

foreach ($arr as $findme) {
	if (strpos($_SERVER['HTTP_HOST'], $findme) !== false) {
		$pos = strpos($_SERVER['HTTP_HOST'], $findme);
		break;
	}
}

$pos = $pos - strpos(substr($_SERVER['HTTP_HOST'], $pos-6, 6);
$domname = substr($_SERVER['HTTP_HOST'], 0, $pos);
$domname = substr($domname, strrchr($domname,'.')+1);

echo $domname;
(untested)
 
Last edited:
Due to .ac, .com, .info, .travel, .museum, .whogizzafuck the scripts was all failing on full tests.

My original 100 line function, used a multidimentional array, where I exploded the URL on a period, then tested the highest element of the array against tld[x], then tested tld[x][y] on highest -1, to see if that existed on the second level, if it was a positive then I accepted url+tld[x][y]+tld[x], else I assumed url+tld[x], then I performed a DNS look up to test it, where URL = part -3 of the exploded url.

However it was a nightmare finding all the official subs to build the multi-dim and it slowed the script down doing look ups and stuff so I went all slasher movie and hacked it up :)
 
Goodness me. Either I'm not understanding this right, or you are all handling this wrong :D

The below code will take an array of domains in a variety of formats, and will get you just the domain element (i.e. no subdomains, no TLDs).

Let me know how you get on:

PHP:
$domains = array("www.test.com","www.test2.co.uk","www.test3.info","www.test4.wz","www.test5.museum","http://test6.co.uk","test7.co.uk","http://www.test8.co.uk");

foreach($domains as $domain) {
    if(preg_match("/\.?([a-z0-9-]+)\.co\.[a-z]{2}$/i",$domain,$m)) {
        //we've matched a ******.co.** domain
        $dom = $m[1];
    } else {
        //treat the domain as a .com/net/org/ws/biz/info
        preg_match("/\.?([a-z0-9-]+)\.[a-z]{2,}$/i",$domain,$m);
        $dom = $m[1];
    }
    echo $domain.': '.$dom.'<br />';
}

Output for the above code:

www.test.com: test
www.test2.co.uk: test2
www.test3.info: test3
www.test4.wz: test4
www.test5.museum: test5
http://test6.co.uk: test6
test7.co.uk: test7
http://www.test8.co.uk: test8
 
Whizz, we are talking about returning ONLY the domain.ext with no subdomains.

Checking if its a valid domain is easy, but slicing the www. or del. or whatever else off the end of the start of a domain is no easy task.

If you knew for a fact that you would only ever get www. you'd just reg replace, but on a parking script ideally you want wildcard sub domains but need a way to pull your database entry

select * from domains where dom = del.icio.us
select * from domains where dom = http://www.icio.us
select * from domains where dom = http://icio.us
select * from domains where dom = v.icio.us

would all return different data and be useless for domain dependant data.

Now because the .ext's can be between 2 and 7 chars long and between 1-2 sections (.com vs .co.uk), it makes separating them hard work.

If you have a nice easy way I'd love to see it :p
 
I'm no PHP expert but surely Jees script does almost what you're asking, it currently returns the main part of the domain, without subdomains or the extension. You just need to alter it slightly to include the extension?

Grant
 
Also, again 'im no php expert' but can't you do this with explode?

Get the domain
explode it by "."
If the last part of the returned array contains "uk" then your domain with extension is made up of the last 3 parts of the array
If it doesn't contain "uk" the last two parts of the array are what you want

Or maybe I'm making this up as I go along :)

Grant
 
Whizz, we are talking about returning ONLY the domain.ext with no subdomains.

That's not what Purg asked:

what bit do you want specifically? Just the 'test' in www.test.co.uk ?

Whizz.... Correct

---

To return the domain name, no matter the subdomain level, try this. It's not 100% perfect, but will cover 99% of all domains:

PHP:
<?php

$domains = array("www.test.com","www.test2.co.uk","www.test3.info","www.test4.wz","www.test5.museum","http://test6.co.uk","test7.co.uk","http://www.test8.co.uk","really.long.sub.domain.here.test9.org.uk");

foreach($domains as $domain) {
    if(preg_match("/\.?([a-z0-9-]+)(\.(co|org|me|ltd|plc)\.[a-z]{2})$/i",$domain,$m)) {
        //we've matched a ******.co.** domain
        $dom = $m[1];
        $dom2 = $m[1].$m[2];
        //print_r($m);
    } else {
        //treat the domain as a .com/net/org/ws/biz/info
        preg_match("/\.?([a-z0-9-]+)(\.[a-z]{2,})$/i",$domain,$m);
        $dom = $m[1];
        $dom2 = $m[1].$m[2];
    }
    echo $domain.' ==> '.$dom2.' --> '.$dom.'<br />';
}
?>

www.test.com ==> test.com --> test
www.test2.co.uk ==> test2.co.uk --> test2
www.test3.info ==> test3.info --> test3
www.test4.wz ==> test4.wz --> test4
www.test5.museum ==> test5.museum --> test5
http://test6.co.uk ==> test6.co.uk --> test6
test7.co.uk ==> test7.co.uk --> test7
http://www.test8.co.uk ==> test8.co.uk --> test8
really.long.sub.domain.here.test9.org.uk ==> test9.org.uk --> test9
 
My bad,

I was concentrating on

Ive also noticed $_SERVER['SERVER_NAME'] sometimes contains WWW. that needs stripping.

Assuming that it was the same sub.dom problem that keeps rearing its ugly head.

that 1% is what pisses me off :p.

I haven't tested this but looking at the regex (and i suck as regex), won't things like ?.uk.com, ?.gb.com and the funky names that likes still cause a fail (altho for the ?.gb/eu/uk/etc.com, an OR op like the co/org/etc could be added. (edited as I missed some of the regex ".[a-z]{2})" coz it went off screen, so that fixes the co.cctld).

Are there any other common ?.ccTLD ? I can only think of gov/plc/ltd/org/co/me/net/ac/edu, but they would be easy to add in to your code anyway. (edited again)

Anyway this is why I gave up and went slasher movie for my needs :p

p.s. sorry for jacking your thread your purg.
 
Last edited:
Whizz... looks good for my needs. My site is almost complete for testing so will include your snippet before going live.

Skinner.... No problem, from your posts ive seen more into the problem than first thought. If the snippet posted by Whizz could be expanded to cover your needs everyone's a winner. Its this kind of post this keep me coming back to the AD forum. The AD community highlight flaws but also follow up by creating the fix for everyone to use.

(Also helps AD SEO which bring fresh meat into the mix)
 
I haven't tested this but looking at the regex (and i suck as regex), won't things like ?.uk.com, ?.gb.com and the funky names that likes still cause a fail (altho for the ?.gb/eu/uk/etc.com, an OR op like the co/org/etc could be added. (edited as I missed some of the regex ".[a-z]{2})" coz it went off screen, so that fixes the co.cctld).

Are there any other common ?.ccTLD ? I can only think of gov/plc/ltd/org/co/me/net/ac/edu, but they would be easy to add in to your code anyway. (edited again)

It won't catch the .uk.com TLD's etc. It could be extended to do these.

The above is merely just a starting point :)
 
I will have a go at the .uk.com SLD's another time.

if anyone uses the above, and wants to make a donation - please do so to Admin
 
Hi Guys,

Keep in mind that eregi will be deprecated as of PHP 5.3.0 and completely removed as of PHP 6 ;)

A better solution would be PHP's parse_url function

PHP:
$url = 'http://username:password@hostname/path?arg=value#anchor';
$parsed_url = parse_url($url); // This would return an array

print_r($parsed_url);

This would output

PHP:
Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

If you wanted to output any of the values by themselves e.g. hostname

PHP:
echo $parsed_url['host'];

or you can output a single value straight from the function

PHP:
echo parse_url($url, PHP_URL_HOST); // This will output hostname

More info on the parse_url function can be found here

As for the OP's problem, you would then further process the output using something like Jeewhizz's solution above.

Hope this helps!

Craig
 
Last edited:
Status
Not open for further replies.

The Rule #1

Do not insult any other member. Be polite and do business. Thank you!

Featured Services

Sedo - it.com Premiums

IT.com

Premium Members

AucDom
UKBackorder
Be a Squirrel
Acorn Domains Merch
MariaBuy Marketplace

New Threads

Domain Forum Friends

Other domain-related communities we can recommend.

Our Mods' Businesses

Perfect
Service
Laskos
*the exceptional businesses of our esteemed moderators
Top Bottom