Is there a simpler way of validating an URL?
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "Invalid URL";
}
Is there a simpler way of validating an URL?
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "Invalid URL";
}
User the FILTER_VALIDATE_URL with the filter_var method:
filter_var docs: http://nl1.php.net/filter_var
filters you can use, including URL http://nl1.php.net/manual/en/filter.filters.validate.php
<?php
$url = "http://www.myweb.com";
if(!filter_var($url, FILTER_VALIDATE_URL))
{
echo "Not valid URL";
}
else
{
echo "Valid URL";
}
?>
You may try this
/^HTTP|HTTPS|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/