Parse a URL in PHP to return domain

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

RJ

Domain BuyerTop Member
Impact
3,225
Please write a short php function to parse a URL and return just the domain name.

Example:
Code:
$url="http://www.namepros.com/showthread.php?p=350493";

$domain = getdomain($url); # domain equals "namepros.com"

Offering a new domain registration.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
<? function getdomain($url)
{
$explode = explode(".", $url);
$tld = $explode[2];
$tld = explode("/", $tld);
$name = $explode[1];
print("$name.$tld[0]");
}

print(getdomain("http://www.namepros.com/showthread.php?p=350493"));

?>

this should work.
 
1
•••
wait working on a better one RJ :D
 
1
•••
PHP:
//Copyright 2004 by Anand A. For authorized use by -RJ- Only :D
// [url]www.polurnet.com[/url]
/*=================================
----EDIT WITH DESIRED URL BELOW--- */

$myurl = "http://www.polurnet.com"

//=====NO More Editing Below Required!==========

function parse_url_domain ($url) {
$parsed = parse_url($url);
$hostname = $parsed['host'];
return $hostname;
}

$raw_url = parse_url($myurl);
$domain_only =str_replace ('www.','', $raw_url);
echo $domain_only['host'];

Example:

$myurl = "http://www.polurnet.com";

Returns
Code:
polurnet.com

The best custom function, and never fails :D Tested for 1 hour too!

Cheers,
Anand
 
Last edited:
1
•••
Would have taken this on if I saw it earlier :) Nice function Polur!
 
0
•••
VERSION 2.0

Example:

$myurl = "http://subdomain.mycrazydomain.anytld";

Returns
Code:
mycrazydomain.anytld


PHP:
//Copyright 2004 by Anand A. For authorized use by -RJ- Only :D
// www.polurnet.com
// VERSION 2.0  -- ANY SUBDOMAIN REMOVAL!

function parse_url_domain ($url) {
$parsed = parse_url($url);
$hostname = $parsed['host'];
return $hostname;
}

$raw_url = parse_url("http://subdomain.mycrazydomain.anytld");
preg_match ("/\.([^\/]+)/", $raw_url['host'], $domain_only);
echo $domain_only[1];

:cy:
 
0
•••
VERSION 2.5

USING FAST REGULAR EXPRESSION SEARCH ONLY!

PHP:
//Copyright 2004 by Anand A. For authorized use by -RJ- Only :D
// www.polurnet.com

//SET URL HERE
$myurl = "http://subdomain.nameprosrules.tld/index.html";

// get host name
preg_match("/^(http:\/\/)?([^\/]+)/i",
   $myurl, $domain_only);
$host = $domain_only[2];
echo "Domain Name is:  " . $host;

output:
domain name is: nameprosrules.tld
 
0
•••
Ideally, it should be able to handle all types of domains, including .co.uk and URLS that contain uppercase letters (http://WWW.NamePros.com/ returns namepros.com)

So far, this one seems to be working good. This was based off Polur's first submission. I was using a function similar to axilant's explode method before but those third level ccTLDs would trip it up.

PHP:
function getdomain($url) { 
$parsed = parse_url($url); 
return str_replace('www.','', strtolower($parsed['host'])); 
}

See any problems with that? Simply want to give it a url at anytime and return the domain name associated with it, ie:

$domain = getdomain($url);
 
0
•••
Version 2 and Version 2.5 are what I recommend RJ. They handle ALL SUDOMAINS (meaning, will remove, even if 'www', or 'blablah' or 'forum', etc.) It will also accept ALL TYPES of TLDS... you name it you got it. That's why the examples are posted with the 'anytld' as the extension :D

Version 1 will only change 'www' which is not an ideal situation

Version 2.5 would be the fastest processing version, as only a complicated, albeit fast regular expression operation is performed. Only thing is that I didn't make it a function, but let me know

You pick :D

BTW: All case INSENSITIVE I believe, if not, let me know :wave:
 
0
•••
Could you put that in a function that handles it all?

Template for it,

PHP:
<?
function getdomain($url) {

#
# your function here
#

return $domain;
}

$url1 = "http://WWW.NAMEPROS.COM/showthread.php?t=53456";
$url2 = "https://www.direct.gov.uk/Homepage/fs/en";  
$url3 = "http://horribly.long.subdomains.file.net/index.php";
$url4 = "http://66.98.205.16/tester";  # just for fun

echo "<br>url: $url1 <Br>domain: ".getdomain($url1)."<br>";
echo "<br>url: $url2 <Br>domain: ".getdomain($url2)."<br>";
echo "<br>url: $url3 <Br>domain: ".getdomain($url3)."<br>";
echo "<br>url: $url4 <Br>domain: ".getdomain($url4)."<br>";
?>
 
0
•••
::Proof of All Domain (Even multi-dots) Compatibility with Version 1.0, 2.0, 2.5::

Say my URL is "http://forum.nameprosrules.co.uk"

Returns: "nameprosrules.co.uk" !!!
=========

For Case Insensitivity, Modify above last echo line by:

{Version 2.0}
PHP:
echo strtolower($domain_only[1]);

{Version 2.5)
PHP:
echo "Domain Name is:  " . strtolower($host);

Hope it helps

Cheers,
Anand
 
0
•••
-RJ- said:
Could you put that in a function that handles it all?

Template for it,

Actually, on second thought, Version 2.0 (using combo of regexp and function) would remain the most stable. The 'fastest' one actually may cause unexpected troubles, as it's a PURE regexp, with strict rules. Thus, stick with the second one, with all function being coded. And the above post also verifies all cases possible.
 
0
•••
Hold on, I'm going to modify the function, so that only the URL needs to be inputted, and no preg_match is done outside of function :D
 
0
•••
PolurNET said:
Hold on, I'm going to modify the function, so that only the URL needs to be inputted, and no preg_match is done outside of function :D

Nice, thanks. The less that needs to be done outside the function, the better so I can just through a .getdomain($url). in my code anywhere.
 
0
•••
Final Version 2.70 - The Best!

Version 2.70

PHP:
//Copyright 2004 by Anand A. For authorized use by -RJ- Only
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// -----  FIND DOMAIN NAME ONLY (HOSTNAME) GIVEN ANY TYPE OF URL OR TLD  -------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// | ADVERTISEMENT | Visit : www.polurnet.com
// VERSION 2.70  -- Integrated to One Function... Input any URL and go!!

function parse_url_domain ($url) {
$raw_url= parse_url($url);
preg_match ("/\.([^\/]+)/", $raw_url['host'], $domain_only);
return strtolower($domain_only[1]);
}

/// Put URL Of Intrest Here :::   ///
$myurl = parse_url_domain ("http://subdomain.mycrazydomain.any.tld/index.php");

/// Output : Case  lowered, only domain + tld shown

echo $myurl;

// Enjoy!


Okay, the above is the final version, all integrated, and 100% working :hehe:

The function that you should call to execute the parsing: parse_url_domain() (Input: Mixed String, Output: Mixed String)
Let me know if you need anything else, or more help :)

Cheers,
Anand
 
Last edited:
0
•••
I just took an example off php.net and modified it to grab domains out of httpS URL's as well as http and put it in a function how I think you wanted it. It would probably just take a simple modification to the regex if you wanted to ignore parsing URL's which are just ip addresses.

PHP:
<? 

function getdomain($url) {

    preg_match (
        "/^(http:\/\/|https:\/\/)?([^\/]+)/i",
        $url, $matches
    );

    $host = $matches[2]; 

    preg_match (
        "/[^\.\/]+\.[^\.\/]+$/", 
        $host, $matches
    );
    
    return strtolower("{$matches[0]}");
} 

$url1 = "http://WWW.NAMEPROS.COM/showthread.php?t=53456"; 
$url2 = "https://www.direct.gov.uk/Homepage/fs/en";   
$url3 = "http://horribly.long.subdomains.file.net/index.php"; 
$url4 = "http://66.98.205.16/tester";  # just for fun 

echo "<br>url: $url1 <Br>domain: ".getdomain($url1)."<br>"; 
echo "<br>url: $url2 <Br>domain: ".getdomain($url2)."<br>"; 
echo "<br>url: $url3 <Br>domain: ".getdomain($url3)."<br>"; 
echo "<br>url: $url4 <Br>domain: ".getdomain($url4)."<br>";

?>
 
0
•••
Nice, I would have replied too had I seen this but I'm a newb here :)
 
0
•••
deadserious, yup, that's my version 2.5 above, but I didn't notice php.net already having it, so I made my own :D
 
0
•••
HI

Interesting thread it seems very close to what I am looking for a code that will support .co.uk domain but what is the Var$ I need to use to do the following

$myurl =$_SERVER["HTTP_HOST"];

I tried the following,

$myurl = parse_url_domain($_SERVER["HTTP_HOST"]);

and also

$domain = $_SERVER["HTTP_HOST"];
$myurl = parse_url_domain($domain);

echo $myurl;

But it did not work, any ideas.

Reason why I want this to work is that I have multiple domains parked as one website with the same dns and I want the domain the visitor has in their browser to load in a special popunder I have...

I have it working with all ext but .co.uk or any thing with a multi ext on the end of the domain.

Do I make sense if not sorry Im still a newbie to PHP

Thanks
Tom Dahne

Expire Domain Software
http://www.expireddomainsoftware.com
 
0
•••
Hi Tom,

If I'm not mistaken it might be caused by a lack of quotes around the server call code, when defining $domain.

But I'll try with the server variable you said, and get back to you :)

:hehe: if you liked this code too, I'd appreciate a small donation of NPs if you don't mind :)

Cheers
Anand
 
0
•••
Olitt — high-converting AI websites, only from $1/moOlitt — high-converting AI websites, only from $1/mo

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover
URLs.com
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back