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

  1. function matchdomain($q){
  2. 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);
  3. return $matches[0];
  4. }
  5. //实例应用,代码如下:
  6. $com = 'www.phpfensi.com';
  7. echo matchdomain($com);
  8. //www.phpfensi.com
  9. $cn = 'afdsafs';
  10. echo matchdomain($cn);
  11. //notice: undefined offset: 0 因为没有不是域名所以正则不到域名所以出错了。