Hi guys , here I am going to share a simple php function which is taken care of our fixing url/links problem by adding http in the url If it does not exists in the url
This function will add http in front of urls which doesnt have.
Contents
Here is the PHP code
function fix_url($url) {
if (substr($url, 0, 7) == 'http://') { return $url; }
if (substr($url, 0, 8) == 'https://') { return $url; }
return 'http://'. $url;
}
Usage
echo fix_url('www.google.com');
Output
http://www.google.com
The above solution is very simple and straight forward solution, But parse_url function in php is the best one to find the protocol.
Please don’t forget to share and subscribe to latest updates of the blog. Comments and feedbacks are always welcome!
Thanks!
Your code is helpful. I just released a more advanced version with some more features. I used same kind of code than yours to get url information of an web address
Thank you Ipsita..
Could you please share the link?