Forum Moderators: coopster

Message Too Old, No Replies

Need to parse out domain name

         

vphoner

8:38 pm on Feb 17, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi, I am a novice at PHP programming. Have done some scripts over the last year. I have the need to parse out the raw domain name from a site URL. Just the domain name, and not any subdomains. I would appreciate it if someone could post the entire code to return a variable with just the domain name.

Example:

Say the domain is [cnet.com...]

I need this to return the variable with "cnet.com"

Say the domain is [reviews.cnet.com...]

I need this to return the variable with "cnet.com"

So basically I am passing the parameter of the long string of the domain and want returned the "site.com" or "site.net" domain name (without anything else).

Thanks in advance.

henry0

9:30 pm on Feb 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



from the
MANUAL
[us.php.net]

<?php
$url = 'http://www.webmasterworld.com/stuffs/anyfile.php';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

another way would be to use a regex
but this could get complicated due to multiple ways of forming an URL

vphoner

12:02 am on Feb 18, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for the info. It got me part of the way there, and by searching on some of that info, I came up with the perfect solution:

[sitepoint.com...]
========================================================
Hey,
I want to track all the domains that refer visitors to my site.
ut thing is I don't want for example, both [google.com...] and [google.com...] in my mysql database.
I just want google.com
What would be the best way to do this?
I don't need the subpage but the subdomain yes.
For example video.google.com and google.com being 2 different referrals.
My question is how I convert the referral into the (sub)domain?

Few ways I've found so far, but I don't know witch one is the best/most reliable:

Using preg_match:
PHP Code:

<?php
$url = "http://www.website.com";
preg_match("/^(http:\/\/)?([^\/]+)/i" , $url, $found);
preg_match("/[^\.\/]+\.[^\.\/]+$/" , $found[2], $found);

echo "domain found: ".$found[0]."<br />";
?>