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

Forum for anything hard- or software related.

Moderator: Moderators

Post Reply
Deimos

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

Post 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]
Rallu
hit the ground hard, real hard
Posts: 93
Joined: Thu Jul 31, 2003 11:36 pm
Location: fi
Contact:

Post 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 ;)
Post Reply