php匹配网址的正则 php匹配网址的正则 几乎可以匹配任何网址
人气:0想了解php匹配网址的正则 几乎可以匹配任何网址的相关内容吗,在本文为您仔细讲解php匹配网址的正则的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:php,匹配网址,下面大家一起来学习吧。
比较简单的
$str = ''; $isMatched = preg_match('/^^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+$/', $str, $matches); var_dump($isMatched, $matches);
几乎可以匹配任何网址:
^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|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}([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_]*)?$
实例:
<script type="text/javascript"> function IsURL(urlString) { regExp = /^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|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}([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_]*)?$/ if (urlString.match(regExp)) return "is a url"; else return "not a url"; } url = ""; alert(IsURL(url)); </script>
加载全部内容