是否有更简单的验证网址的方法?
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "Invalid URL";
}
答案 0 :(得分:3)
使用filter_var方法使用FILTER_VALIDATE_URL:
filter_var docs: http://nl1.php.net/filter_var
您可以使用的过滤器,包括网址 http://nl1.php.net/manual/en/filter.filters.validate.php
答案 1 :(得分:3)
<?php
$url = "http://www.myweb.com";
if(!filter_var($url, FILTER_VALIDATE_URL))
{
echo "Not valid URL";
}
else
{
echo "Valid URL";
}
?>
答案 2 :(得分:1)
你可以试试这个
/^HTTP|HTTPS|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/