Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a website´s URL as a string with http:// upfront

I have a comment system that allows auto linking of url. I am using cakephp but the solution is more just PHP. here is what is happening.

if the user enters fully qualified url with http:// or https:// everything is fine. but if they enter www.scoobydoobydoo.com it turns into http://cool-domain.com/www.scoobydoobydoo.com. basically cakephp understands that http|https is an external url so it works with http|https not otherwise.

My idea was to do some kind of str stuff on the url and get it to insert the http if not present. unfortunately whatever i try only makes it worse. I am noob :) any help / pointer is appreciated.

thanks

EDIT: posting solution snippet. may not be the best but thanks to answer at least I have something.

<?php
        $proto_scheme = parse_url($webAddress,PHP_URL_SCHEME);
    if((!stristr($proto_scheme,'http')) || (!stristr($proto_scheme,'http'))){
        $webAddress = 'http://'.$webAddress;
    }
?>
like image 988
Abhishek Dujari Avatar asked Dec 04 '22 23:12

Abhishek Dujari


2 Answers

$url = "blahblah.com";
// to clarify, this shouldn't be === false, but rather !== 0
if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) {
   $url = "http://{$url}";
}
like image 116
Michael McTiernan Avatar answered Dec 10 '22 09:12

Michael McTiernan


Try the parse_url function: http://php.net/manual/en/function.parse-url.php

I think this will help you.

like image 23
Tobias Avatar answered Dec 10 '22 09:12

Tobias