Home  >  Article  >  Backend Development  >  php如何验证url地址是否合法

php如何验证url地址是否合法

PHPz
PHPzOriginal
2016-06-13 10:07:503861browse

php如何验证url地址是否合法

php验证url地址是否合法

php开发过程中,常常需要验证用户提交的URL地址是否合法,也就是判断是不是URL地址。下面分享一下,正则验证URL地址是否合法的方法。

首先,定义函数check_url:

<?php
function check_url($url){
    if(!preg_match(&#39;/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is&#39;,$url)){
        return false;
    }
    return true;
}
?>

使用方法:

<?php
$url=&#39;需验证的url&#39;;
$isurl=check_url($url);
if($isurl){
    echo &#39;url地址合法&#39;;
}else{
    echo &#39;url地址非法&#39;;
}
?>

更多相关技术文章,请访问PHP中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn