Hey,
I have a string that reads "http://www.domain.com/index/a/b". How would I go about stripping that so that it's only "domain.com"?
I've tried a ton of stuff but can't come up with anything. Could anyone chime in?
Regards
| SitePoint Sponsor |
Hey,
I have a string that reads "http://www.domain.com/index/a/b". How would I go about stripping that so that it's only "domain.com"?
I've tried a ton of stuff but can't come up with anything. Could anyone chime in?
Regards
should work for any legitimate url:
PHP Code:$url = 'http://www.domain.com/index/a/b';
preg_match("#^(?:http://)?(?:www\.)?([\w\.\-]+)#", $url, $matches);
$domain = $matches[1];


PHP Code:$url = 'http://www.domain.com/index/a/b';
$tld = parse_url($url);
$tld = preg_replace('/^www\./', '', $tld['host']);
echo $tld;
Edit:
aamonkey was faster. I like his code better too, does the job with one function.
Dang, thanks. I really need to start learning regex.


Actually I'm wondering about what the characters in blue are for? Could you explain those? Thanks.
^(?:http://)?(?:www\.)?([\w\.\-]+)
the ?: at the start of a subpattern makes that sub-pattern non-capturing. for example, if I have a string '123abc' and i use the pattern (\d+)(\w+) with preg_match, $matches[1] will be 123 and $matches[2] will be abc.
But if I change the pattern to (?:\d+)(\w+) now $matches[1] will be abc because I've told it to ignore the 1st subpattern.
I generally use it when I can as it makes the pattern run a little faster and (i think) saves memory....


Aha, great. I've learnt something new. Thank you!
Bookmarks