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

PHPz
Release: 2020-06-23 11:50:21
Original
3914 people have browsed it

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;
}
?>
Copy after login

使用方法:

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template