开发者

PHP REGEX: Get domain from URL

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             |
+----------------------------------------------+--------开发者_StackOverflow社区---------------+

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

Thanks for any help!


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);


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.


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


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

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


Assumes that http:// prefixes everything.

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


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 ;-)


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.


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


Best way i think:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜