Forum Moderators: coopster

Message Too Old, No Replies

How to extract the domain name from a URL

         

Gian04

7:40 am on Jun 11, 2007 (gmt 0)

10+ Year Member



preg_match('@^(?:http://)?([^/]+)@i', $inputtext, $matches);
$host = $matches;

preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";

The code above will return the correct domain is the user input is something like http://www.example.com

But if the input is http://www.example.co.uk it will return co.uk

also it will accept an input like abc@.com (will return abc@.com as domain)

Anyone who has a better solution?

[1][edited by: Gian04 at 7:42 am (utc) on June 11, 2007]

dreamcatcher

8:03 am on Jun 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, misread your post.

dc

Gian04

8:11 am on Jun 11, 2007 (gmt 0)

10+ Year Member



dc, please explain how can I implement this, it just display my own domain name.

What I want to do is to check for the domain in a user input URL and to be able to check if its already present in the database.

thanks

whoisgregg

1:49 pm on Jun 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about using parse_url() [php.net]?