2011-01-30 208 views
0

我有一个字符串,可能是一个URL。我想通过正则表达式来判断它是否是一个Http/FTP URL。如果字符串是有效的URL,那么我会让它超链接和蓝色等,如果它不是基于ture或假的正则表达式的URL我想要进一步决定进一步..URL验证的正则表达式

什么是最好的正则表达式?

+0

[URL用于PHP的验证/正则表达式]的可能重复(HTTP://计算器.com/questions/206059/php-validation-regex-for-url) – 2011-01-30 04:57:07

+2

你有没有Google搜索“URL正则表达式”?什么味道正则表达式/为哪种语言? – 2011-01-30 04:57:21

回答

0

这个Perl代码相当准确,可能不完美。第1组中含有两种httpftp

if ($str =~ /^(http|ftp):\/\/[\w.%@]+(:?[?].*)?/) { 
    print "matches $1\n"; 
} else { 
    print "no match\n"; 
} 
0

以防万一你想知道的网址确实存在:

function url_exist($url){//se passar a URL existe 
    $c=curl_init(); 
    curl_setopt($c,CURLOPT_URL,$url); 
    curl_setopt($c,CURLOPT_HEADER,1);//get the header 
    curl_setopt($c,CURLOPT_NOBODY,1);//and *only* get the header 
    curl_setopt($c,CURLOPT_RETURNTRANSFER,1);//get the response as a string from curl_exec(), rather than echoing it 
    curl_setopt($c,CURLOPT_FRESH_CONNECT,1);//don't use a cached version of the url 
    if(!curl_exec($c)){ 
     //echo $url.' inexists'; 
     return false; 
    }else{ 
     //echo $url.' exists'; 
     return true; 
    } 
    //$httpcode=curl_getinfo($c,CURLINFO_HTTP_CODE); 
    //return ($httpcode<400); 
}