正则匹配所有url

正则匹配所有url

测试一个正则,差不多能匹配所有的url。

测试url的数组

$url_arr=array(

"https://www.a12.com/url/Url-4-PU-UK-ug-AC/123.html",

"https://www.a12.com/url/url-4-pu-uk-ug-ac/123.html",

"http://www.a12.com",

"https://pan.baidu.com/s/1e11w",

"pan.baidu.com/s/1E11W",

"pan.baidu.com/s/1e11w",

'ftp://166.111.161.47',

'ftp.math.cuhk.edu.hk',

'www.baidu',

);

正则

$reg='/^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|';

$reg.='gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}';

$reg.='([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-z][a-z0-9_]*)?$/';

便利匹配

$str='';

foreach ($url_arr as $k => $v) {

$str .= preg_match($reg,$v);

}

var_dump($str);

结果

正则匹配所有url