Things I want

I wish to get from the URL the domain part so from ->

Good examples:


 input                                         output                


 http://world wide    world wide             

 http://world wide                world wide        

 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.


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