Page 1 of 1

php: Add http:// if not entered in a form

Posted: Wed Dec 03, 2003 12:24 pm
by Deimos
I had a small problem with some websites lately, according to the databases, people started to fill in webaddresses without http:// and some with http:// so i coded the following to fix this.



[php]

<?php



$url = "www.yourdomain.net";



function CheckUrl($url)

{



$fls = substr($url,0,7);

$ucase_var = strtoupper($fls);



if($ucase_var != "HTTP://")

{

$url = "http://$url";

}



return($url);



}



$result = CheckUrl ($url);



echo $result;



?>[/php]

Posted: Thu Dec 04, 2003 4:05 am
by Rallu
This is bit shorter version ;)



[php]<?php

$url = "www.yourdomain.net";

function CheckUrl($url)

{

if (!preg_match("/http:\/\/\b/i",$url))

return "http://$url";

return $url;

}

echo CheckUrl($url);

?>[/php]



Edit: Removed one more line ;)