.Darkman Posted March 20, 2007 Share Posted March 20, 2007 Hi, I need a small help. How to strip a domain name alone from an URL. For eg, User enters http://www.phpfreaks.com/forums/index.php?action=post;board=1.0 in a field. I need that to be converted into http://www.phpfreaks.com/ How do i do it ? Also how do i make so that http://phpfreaks.com/ is converted into http://www.phpfreaks.com/ Thanks, Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 20, 2007 Share Posted March 20, 2007 you need to look into split(), str_replace, and strpos(). you will always get use out of regex. Quote Link to comment Share on other sites More sharing options...
btherl Posted March 20, 2007 Share Posted March 20, 2007 You might want to use parse_url(). It's much less effort. http://sg.php.net/manual/en/function.parse-url.php Quote Link to comment Share on other sites More sharing options...
.Darkman Posted March 20, 2007 Author Share Posted March 20, 2007 You might want to use parse_url(). It's much less effort. http://sg.php.net/manual/en/function.parse-url.php Thanks a lot for this. Now how do i convert phpfreaks.com to www.phpfreaks.com Thanks, Quote Link to comment Share on other sites More sharing options...
btherl Posted March 20, 2007 Share Posted March 20, 2007 $str = 'phpfreaks.com'; if (strpos($str, 'www') !== 0) { # !== 0 fails only if 'www' is found at the start of $str $str = "www.$str"; } I deliberately searched for 'www' instead of 'www.', to allow for cases like www1, www2. I often use a regex like this instead if (!preg_match('|www[0-9]*\.|', $str)) That one matches www. www1. www50. And all other numbers following www Quote Link to comment Share on other sites More sharing options...
.Darkman Posted March 20, 2007 Author Share Posted March 20, 2007 Thanks a lot Quote Link to comment 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.