Imad Posted July 28, 2008 Share Posted July 28, 2008 Hi guys, I need help with getting a domain from a url. Say the url was a domain like this: http://google.com/google.html I converted it to using a pregmatch: http://google.com The only problem I have is with subdomains and co.uk urls as well as with subdomains such as: http://go.google.com/google.html to make it: http://go.google.com And: http://go.google.co.uk Or: http://www.go.google.co.uk to end up as: http://go.google.co.uk I made this code which is good for domains without subdomains: $urls = 'http://google.com/google.com'; $regexmatch = '/.((([^.]+)\.){1})([a-zA-Z]{3,}|[^?]|[a-zA-Z.]{5,})/'; preg_match($regexmatch, "$urls", $matches); Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/ Share on other sites More sharing options...
dezkit Posted July 28, 2008 Share Posted July 28, 2008 "I made it get it as: http://gooogle.com" wat Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601865 Share on other sites More sharing options...
Imad Posted July 28, 2008 Author Share Posted July 28, 2008 "I made it get it as: http://gooogle.com" wat I should have clarified it better. I converted it to http://google.com I did misspell google, so I fixed it in my first post. Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601868 Share on other sites More sharing options...
DarkWater Posted July 28, 2008 Share Posted July 28, 2008 <?php $url = "http://www.google.com/google.html"; if (substr($url, 0, 7) == "http://") { $slashpos = strpos($url, '/', ; } else { $slashpos = strpos($url, '/'); } if ($slashpos) { $domain = substr($url, 0, $slashpos); } else { $domain = $url; } echo $domain; ?> I could have written it better, but it gets the job done. Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601869 Share on other sites More sharing options...
Imad Posted July 28, 2008 Author Share Posted July 28, 2008 Thanks Darkwater, that did it. I wanted to do a pregmatch for a forward slash but that doesn't work. Now I know a new function that would actually make it work. Thanks again, Best Regards. Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601874 Share on other sites More sharing options...
papaface Posted July 28, 2008 Share Posted July 28, 2008 Maybe: <? $urls = 'http://go.google.com/google.com'; echo pathinfo($urls,1); ?> Is that what you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601890 Share on other sites More sharing options...
Imad Posted July 28, 2008 Author Share Posted July 28, 2008 Maybe: <? $urls = 'http://go.google.com/google.com'; echo pathinfo($urls,1); ?> Is that what you are looking for? no, I was actually looking for something that would break a url down to the domain and provide only the domain. Best Regards. Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601892 Share on other sites More sharing options...
papaface Posted July 28, 2008 Share Posted July 28, 2008 Thats what it does ??? Produces http://go.google.com Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601896 Share on other sites More sharing options...
Imad Posted July 28, 2008 Author Share Posted July 28, 2008 Yes, but I modified what DarkWater gave me to add http:// if it doesn't exist and remove www. It can be done with yours as-well. There are so many ways to do things. Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601899 Share on other sites More sharing options...
DarkWater Posted July 28, 2008 Share Posted July 28, 2008 So mark this topic as "solved" I guess. =P Quote Link to comment https://forums.phpfreaks.com/topic/117018-solved-geting-domain-from-url-help/#findComment-601901 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.