验证一个URL是否合法,常常会用到正则表达式检测该URL是否合法:
$url = '';// http://或者https://if(!preg_match('/http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$url)){echo '该域名不合法';} else {echo '该域名合法';}
在助手类中封装成一个方法:
/*** 判断域名是否合法* @return string*/public static function checkUrl($url){if(!preg_match('/http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$url)){return false;}return true;}
每次需要使用的时候直接调用,简单快捷
if(!Utils::checkUrl($longUrl)){echo "该域名不合法";}