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. 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 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. 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. 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. 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? 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. 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 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. 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 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
Archived
This topic is now archived and is closed to further replies.