Home >php教程 >php手册 >验证URL是否合法

验证URL是否合法

WBOY
WBOYOriginal
2016-06-06 19:35:481359browse

从网上转来的,比较完善, 无 public function isValidUrl() { $patern = '/^http[s]?:\/\/'. '(([0-9]{1,3}\.){3}[0-9]{1,3}'. // IP形式的URL- 199.194.52.184 '|'. // 允许IP和DOMAIN(域名) '([0-9a-z_!~*\'()-]+\.)*'. // 三级域验证- www. '([0-9a-z][

从网上转来的,比较完善,
public function isValidUrl() {

    $patern = '/^http[s]?:\/\/'.  
        '(([0-9]{1,3}\.){3}[0-9]{1,3}'.             // IP形式的URL- 199.194.52.184  
        '|'.                                        // 允许IP和DOMAIN(域名)  
        '([0-9a-z_!~*\'()-]+\.)*'.                  // 三级域验证- www.  
        '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.'.     // 二级域验证  
        '[a-z]{2,6})'.                              // 顶级域验证.com or .museum  
        '(:[0-9]{1,4})?'.                           // 端口- :80  
        '((\/\?)|'.                                 // 如果含有文件对文件部分进行校验  
        '(\/[0-9a-zA-Z_!~\*\'\(\)\.;\?:@&=\+\$,%#-\/]*)?)$/';

    if(!preg_match($patern, $this->url)) {
        $this->addError('url', '您输入的URL格式有问题,请检查!');
    }
}
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