24

What I want


I want to get from a URL the domain part so from http://example.com/ -> example.com

Examples:


+----------------------------------------------+-----------------------+
| input                                        | output                |
+----------------------------------------------+-----------------------+
| http://www.stackoverflow.com/questions/ask   | www.stackoverflow.com |
| http://validator.w3.org/check                | validator.w3.org      |
| http://www.google.com/?q=hello               | www.google.com        |
| http://google.de/?q=hello                    | google.de             |
+----------------------------------------------+-----------------------+

I found some related questions in stackoverflow but none of them was exactly what I was looking for.

Thanks for any help!

Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
  • This code will help you get the complete domain name: https://gist.github.com/praisedpk/64bdb80d28144aa78d58469324432277 – Hamid Sarfraz Sep 18 '16 at 20:27

9 Answers9

82

There's no need to use a regex for this. PHP has an inbuilt function to do just this. Use parse_url():

$domain = parse_url($url, PHP_URL_HOST);
cletus
  • 616,129
  • 168
  • 910
  • 942
  • 2
    it good only if containing http(s) , not for "stackoverflow.com/questions" – ewwink Jan 27 '14 at 05:20
  • 1
    this will also give you subdomains. Be careful because `parse_url('http://example.com', PHP_URL_HOST) == parse_url('http://www.example.com', PHP_URL_HOST)` will return false – Jonathan Morales Vélez May 13 '15 at 22:30
4

I use:

$domain = parse_url('http://' . str_replace(array('https://', 'http://'), '', $url), PHP_URL_HOST);

Because parse_url doesn't return host key when schema is missing in $url.

aniski
  • 1,263
  • 1
  • 16
  • 31
Marcin Żurek
  • 145
  • 1
  • 3
2

This is like the regex from theraccoonbear but with support for HTTPS domains.

if (preg_match('/https?:\/\/([^\/]+)\//i', $target_string, $matches)) {
  $domain = $matches[1];
}
Community
  • 1
  • 1
fnkr
  • 9,428
  • 6
  • 54
  • 61
2
$tmp = parse_url($url);
$url = $tmp['host']
turbod
  • 1,988
  • 2
  • 17
  • 31
1

Assumes that http:// prefixes everything.

$tmp = explode("/", $url);
$domain = $tmp[2];
Josh K
  • 28,364
  • 20
  • 86
  • 132
1

I think the following regexp might answers your question.

This diagram explains how it works, or rather why it works :-)

$regexp = '/.*\/\/([^\/:]+).*/';

// www.stackoverflow.com
echo preg_replace($regexp, '$1', 'http://www.stackoverflow.com/questions/ask');

// google.de
echo preg_replace($regexp, '$1', 'http://google.de/?q=hello');

// it works for the other input tests too ;-)
Eugen Mihailescu
  • 3,553
  • 2
  • 32
  • 29
0

Best way i think:

preg_match('/(http(|s)):\/\/(.*?)\//si',  'http://www.example.com/page/?bla=123#!@#$%^&*()_+', $output);
// $output[0] ------------>  https://www.example.com/
T.Todua
  • 53,146
  • 19
  • 236
  • 237
0

Here's my quick and dirty solution.

http://([^/]+).*

I haven't tested it, but it should grab anything between the http:// and the first slash.

haydenmuhl
  • 5,998
  • 7
  • 27
  • 32
0
if (preg_match('/http:\/\/([^\/]+)\//i', $target_string, $matches)) {
  $domain = $matches[1];
}
theraccoonbear
  • 4,283
  • 3
  • 33
  • 41