Jump to content

[SOLVED] Geting domain from url help


Imad

Recommended Posts

Hi guys, I need help with getting a domain from a url.

Say the url was a domain like this: http://google.com/google.html

I converted it to using a pregmatch: http://google.com

 

The only problem I have is with subdomains and co.uk urls as well as with subdomains such as:

http://go.google.com/google.html

to make it:

http://go.google.com

And:

http://go.google.co.uk

Or:

http://www.go.google.co.uk

to end up as: http://go.google.co.uk

 

I made this code which is good for domains without subdomains:

$urls = 'http://google.com/google.com';

$regexmatch = '/.((([^.]+)\.){1})([a-zA-Z]{3,}|[^?]|[a-zA-Z.]{5,})/';
preg_match($regexmatch, "$urls", $matches);

 

Any help would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/
Share on other sites

<?php
$url = "http://www.google.com/google.html";
if (substr($url, 0, 7) == "http://") {
    $slashpos = strpos($url, '/', ;
}
else {
    $slashpos = strpos($url, '/');
}
if ($slashpos) {
$domain = substr($url, 0, $slashpos);
}
else {
$domain = $url;
}
echo $domain;
?>

 

I could have written it better, but it gets the job done.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.