Home >php教程 >PHP源码 >php 匹配url 正则表达式代码

php 匹配url 正则表达式代码

WBOY
WBOYOriginal
2016-06-08 17:26:531097browse

这是个获取文章内容中所有链接的php正则表达式

<script>ec(2);</script>

$str ="";

 代码如下 复制代码
$reg = "[^"']*))|(?[^s>]+))[sS]*?>";

//下面这个实例是获取内容中域名正则表达式

 代码如下 复制代码
function get_domain($url){
  $pattern = "/[w-]+.(com|net|org|gov|cc|biz|info|cn)(.(cn|hk))*/";
  preg_match($pattern, $url, $matches);
  if(count($matches) > 0) {
   return $matches[0];
  }else{
   $rs = parse_url($url);
   $main_url = $rs["host"];
   if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
    return $main_url;
   }else{
    $arr = explode(".",$main_url);
    $count=count($arr);
    $endArr = array("com","net","org","3322");//com.cn  net.cn 等情况
    if (in_array($arr[$count-2],$endArr)){
     $domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
    }else{
     $domain =  $arr[$count-2].".".$arr[$count-1];
    }
    return $domain;
   }
  }
 }

 
 //实例
 
 $str ="jfkdlajfdafdjak;www.111cn.net";
 echo get_domain($str)
 
 //得出值为111cn.net
 

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
Previous article:php获取字符串uft-8编码Next article:php读写文件函数