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!

1

9 Answers 9

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);
Sign up to request clarification or add additional context in comments.

2 Comments

it good only if containing http(s) , not for "stackoverflow.com/questions"
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
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.

Comments

2
$tmp = parse_url($url);
$url = $tmp['host']

Comments

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];
}

Comments

1

Assumes that http:// prefixes everything.

$tmp = explode("/", $url);
$domain = $tmp[2];

1 Comment

heh, most out-of-the-box solution :-)
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 ;-)

Comments

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.

Comments

0
if (preg_match('/http:\/\/([^\/]+)\//i', $target_string, $matches)) {
  $domain = $matches[1];
}

Comments

0

Best way i think:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.