etrader Posted April 18, 2011 Share Posted April 18, 2011 I have two questions: 1. Is it the best way to get the domain of a url by parse_url? or preg_match or str_replace can be better alternatives? 2. What is the best way to remove www from the domains? Note that the domain name itself may contain www too (like www.1www.com) Thank for your attention Quote Link to comment https://forums.phpfreaks.com/topic/234044-getting-the-domain-of-a-url-by-parse_url/ Share on other sites More sharing options...
ignace Posted April 18, 2011 Share Posted April 18, 2011 1. parse_url is by far the best solution. Avoid preg_match whenever you can. 2. $domain = ltrim(parse_url($url, PHP_URL_HOST), 'w.'); /*magic*/ This can become an issue if your domain is http://www.com (valid host name) but you can solve that like this: $domain = 'www.' . ltrim(parse_url($url, PHP_URL_HOST), 'w.'); /*magic*/ This will do the following: www.com =ltrim=> com => www.com domain.top =ltrim=> domain.top => www.domain.top www.domain.top =ltrim=> domain.top => www.domain.top Quote Link to comment https://forums.phpfreaks.com/topic/234044-getting-the-domain-of-a-url-by-parse_url/#findComment-1203099 Share on other sites More sharing options...
etrader Posted April 19, 2011 Author Share Posted April 19, 2011 Perfect advice! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/234044-getting-the-domain-of-a-url-by-parse_url/#findComment-1203312 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.