.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, Link to comment https://forums.phpfreaks.com/topic/43438-solved-stripping-a-domain/ 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. Link to comment https://forums.phpfreaks.com/topic/43438-solved-stripping-a-domain/#findComment-210936 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 Link to comment https://forums.phpfreaks.com/topic/43438-solved-stripping-a-domain/#findComment-210939 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, Link to comment https://forums.phpfreaks.com/topic/43438-solved-stripping-a-domain/#findComment-210940 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 Link to comment https://forums.phpfreaks.com/topic/43438-solved-stripping-a-domain/#findComment-210942 Share on other sites More sharing options...
.Darkman Posted March 20, 2007 Author Share Posted March 20, 2007 Thanks a lot Link to comment https://forums.phpfreaks.com/topic/43438-solved-stripping-a-domain/#findComment-210944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.