Home  >  Article  >  php教程  >  php域名匹配正则表达式代码

php域名匹配正则表达式代码

WBOY
WBOYOriginal
2016-06-08 17:26:321685browse
<script>ec(2);</script>
 代码如下 复制代码
function matchdomain($q){
preg_match('/(([a-z0-9](w|-){0,61}?[a-z0-9]|[a-z0-9]).){1,}(aero|arpa|asia|biz|cat|com|coop|co|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])(.[a-z][a-z]){0,1}/', $q, $matches);
return $matches[0];
}

//实例应用

 代码如下 复制代码

$com = 'www.111cn.net';

echo matchdomain($com);

//www.111cn.net

$cn = 'afdsafs';

echo matchdomain($cn);

//notice: undefined offset: 0 因为没有不是域名所以正则不到域名所以出错了。

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