itsureboy Posted May 22, 2010 Share Posted May 22, 2010 I need some simple code that would take a URL and convert it to its utmost proper form which would be for example, http://www.site.com So if a user submits the URL short without the http://, or www. the code would add it accordingly. Any help. Thanks ahead. Quote Link to comment https://forums.phpfreaks.com/topic/202614-need-a-simple-url-converter/ Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 Well, you could do if (strncmp($url, 'http://', 7) != 0) { $url = 'http://' . $url; } You're wrong about the www stuff. The domain name www.example.com doesn't need to point to the same place as example.com. Quote Link to comment https://forums.phpfreaks.com/topic/202614-need-a-simple-url-converter/#findComment-1062077 Share on other sites More sharing options...
paddyhaig Posted May 23, 2010 Share Posted May 23, 2010 It's possible you could use a redirect like so, of course putting what you would like in the url. <?php ob_start(); header("Location: http://en.wikipedia.org"); ob_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/202614-need-a-simple-url-converter/#findComment-1062080 Share on other sites More sharing options...
Daniel0 Posted May 23, 2010 Share Posted May 23, 2010 How would redirecting the user take care of normalizing the string? Quote Link to comment https://forums.phpfreaks.com/topic/202614-need-a-simple-url-converter/#findComment-1062082 Share on other sites More sharing options...
Rustywolf Posted May 23, 2010 Share Posted May 23, 2010 or just, if you want. $url = str_replace('http://', $url); $url = 'http://' . $url; But daniel's way sounds better. Quote Link to comment https://forums.phpfreaks.com/topic/202614-need-a-simple-url-converter/#findComment-1062144 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.