Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.