By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

getting domain only

P: n/a
given an url (through a form) like

http://www.com.com/sdsfewa/asadasd/asdasdas
www.nl.sd/sdfsf/sdfds.xml
weblog.lastpak.nl/index.php?id=sdfsd
http://testing.stupid.com
i want to have only the domain (in this case www.com.com, www.nl.sd,
weblog.lastpak.nl and testing.stupid.com),so stripping of the http://
(can be done easily) but also everything behind .com .nl or whatever.
--
http://bluap.nl
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
BluaP wrote:
given an url (through a form) like

http://www.com.com/sdsfewa/asadasd/asdasdas
www.nl.sd/sdfsf/sdfds.xml
weblog.lastpak.nl/index.php?id=sdfsd
http://testing.stupid.com

i want to have only the domain (in this case www.com.com, www.nl.sd,
weblog.lastpak.nl and testing.stupid.com),so stripping of the http://
(can be done easily) but also everything behind .com .nl or whatever.


Hi,

Make sure you add "http://" if not present, and then use parse_url().

http://uk.php.net/manual/en/function.parse-url.php

HTH;
JOn
Jul 16 '05 #2

P: n/a
BluaP wrote:

given an url (through a form) like

http://www.com.com/sdsfewa/asadasd/asdasdas
www.nl.sd/sdfsf/sdfds.xml
weblog.lastpak.nl/index.php?id=sdfsd
http://testing.stupid.com

i want to have only the domain (in this case www.com.com, www.nl.sd,
weblog.lastpak.nl and testing.stupid.com),so stripping of the http://
(can be done easily) but also everything behind .com .nl or whatever.
--
http://bluap.nl


$urls = array("http://www.com.com/sdsfewa/asadasd/asdasdas",
"www.nl.sd/sdfsf/sdfds.xml", "weblog.lastpak.nl/index.php?id=sdfsd",
"http://testing.stupid.com");

foreach ($urls as $url)
echo preg_replace("/^(http:\/\/)?([^\/\?]+)([\/|\?])?(.+)?$/", "\$2\n", $url);

This seems to work. I wasn't sure if it was possible for a url to take the
form:

http://www.domain.com?foo=bar

but it's included anyway.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.