treehouse : what would you like to learn today?
Web Design Web Development iOS Development

URL Validation

Last updated on:

$url = 'http://example.com';
$validation = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);

if ( $validation ) $output = 'proper URL';
else $output = 'wrong URL';

echo $output;
View Comments

Comments

  1. Will
    Permalink to comment#

    $output = ‘proper UL’;
    I think there is a typo in that line.

  2. Emilio CS
    Permalink to comment#

    I think it needs improvements. The code will pass the url “http://www.example” as valid url.

  3. filter_var is good one but I still have faith in good old preg_match() and the regular expressions.

    preg_match(“#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i”,$url);

Leave a Comment

Use markdown or basic HTML and be nice.