Things I want


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

Good examples:


+----------------------------------------------+-----------------------+

 input                                         output                

+----------------------------------------------+-----------------------+

 http://world wide web.stackoverflow.com/questions/request    world wide web.stackoverflow.com 

 http://validator.w3.org/check                 validator.w3.org      

 http://world wide web.google.com/?q=hello                world wide web.google.com        

 http://google.p/?q=hello                     google.p             

+----------------------------------------------+-----------------------+

I discovered some related questions in stackoverflow but not one of them was precisely what I had been searching for.

Thank you for any help!

There's you don't need to make use of a regex with this. PHP comes with an built-in function to complete this. Use parse_url():

$domain = parse_url($url, PHP_URL_HOST)

Assumes that http:// prefixes everything.

$tmp = explode("/", $url)

$domain = $tmp[2]

parse_url has already been a PHP function, you don't need to duplicate it. http://php.internet/manual/en/function.parse-url.php

$tmp = parse_url($url)

$url = $tmp['host']

Here's my fast and dirty solution.

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

I've not examined it, however it should grab anything between your http:// and also the first slash.

if (preg_match('/http://([^/]+)//i', $target_string, $matches))