View Full Version : Check for http:// in a string

11-20-2007, 06:38 PM
I'm trying to figure out how to check a users input for http:// and if its not there insert the http:// Ideas?

11-20-2007, 06:50 PM
If you expect it at the beginning of the string you can do something like this:

$string = 'http://www.google.com';
$string = (substr(ltrim($string), 0, 7) != 'http://' ? 'http://' : '') . $string;

11-20-2007, 07:11 PM
so if the user enters www.blah.com it will end up http://www.blah.com?

11-20-2007, 07:29 PM
To understand Inigoesdr's code you'll want to study these web pages:

substr() (http://us2.php.net/manual/en/function.substr.php)

ltrim() (http://us2.php.net/manual/en/function.ltrim.php)

Ternary operators (http://us2.php.net/manual/en/language.operators.comparison.php) (scroll down til you get to the paragraph on ternary operators)

I don't think you should use any code that you don't understand.

11-20-2007, 07:42 PM
what you do you think is the best way to store links in a database I tried htmlentinty(), special chars. this is how my form processor is handling it

$blog1_url = mysql_real_escape_string(htmlentities($_POST['blog1_url']));