0

I'm getting a url from a form, this way:

$input_website = isset($_POST['website']) ? check_plain($_POST['website']) : 'None';

I need to get back a naked domain name(for some API integration), for example: http://www.example.com will return as example.com and www.example.com will return example.com etc.

I have this code now, that returns the correct url for the first case http://www.example.com but returns nothing for www.example.com or even example.com:

function get_domain($url)
{
    $pieces = parse_url($url);
    $domain = isset($pieces['host']) ? $pieces['host'] : '';
    if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
        return $regs['domain'];
    }
    return false;
}

Can you please advice on the matter?

Noam Gur
  • 65
  • 11

3 Answers3

2

As per discussion with you:

$url = 'www.noamddd.com';
$arrUrl = explode("/", $url);
echo $arrUrl[0];

Old Answer:

Make a function with the following code block and get the domain names.

Try this

more about parse_url

$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
print $parse['host']; //google.com

Also you can do this in another way:

echo $domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));//google.com
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • thanks. iv'e changed the code to be tis 'code' function get_domain($url) { $parse = parse_url($url); return $parse['host']; } $webSite = get_domain($input_website); 'code' but it's coming back empty again. – Noam Gur May 03 '16 at 10:17
  • can you please post your `url`?? – Murad Hasan May 03 '16 at 10:21
  • I'm receiving the url from an html form, this is a url for example that ive tried and came back empty www.noamddd.com – Noam Gur May 03 '16 at 11:32
  • use the url with `http://` like `http://www.noamddd.com`. otherwise it will not a url, just a string. – Murad Hasan May 03 '16 at 11:35
  • As i've said on the question, the function is returning the domain name when the user is return a url with http, my problem is when the user brings the url without http – Noam Gur May 03 '16 at 11:51
  • Unfortunlly, still not working. the new code you wrote brings back www.noamddd.com instead of noamddd.com, and for http://www.noamddd.com it returns http: – Noam Gur May 03 '16 at 14:15
0

If you just have the URL (and not want the current domain name like frayne-konok suggests) and want to extract the server name, you can use a regular expression like this:

$serverName = preg_replace('|.*?://(.*?)/.*|', '$1', $url);
Community
  • 1
  • 1
JSchirrmacher
  • 3,243
  • 2
  • 19
  • 27
0

I ended up doing something a bit different - checking if there is http and if not, i'm adding it using this function:

function addHttp($website) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $website;
}

and only then i'm sending it to my other function that return the domain. For sure not the best way, but it works.

Noam Gur
  • 65
  • 11