首页 > 语言 > PHP > 正文

php 正则字符串中外部链接代码函数

2024-09-04 11:47:34
字体:大 中 小
来源:转载
供稿:网友
  1. function deleteemptyarray( $val ) 
  2. { 
  3.  $links =''; 
  4.  if( is_array( $val ) ) 
  5.  { 
  6.   foreach( $val as $v =>$_v) 
  7.   { 
  8.    if( !emptyempty( $_v[0] ) ) 
  9.    { 
  10.     $links .=$_v[0].'|'; 
  11.    } 
  12.   }   
  13.   return substr($links,0,-1); 
  14.  } 
  15.  else 
  16.  { 
  17.   return false; 
  18.  } 
  19. } 
  20.  
  21. function getoutlink($body) 
  22. { 
  23.  
  24.  $tempcontent = $body; 
  25.  preg_match_all("/<a(.*?)href=(.*?)</a>/i",$tempcontent,$tempurl); 
  26.  $urls =array(); 
  27.  foreach($tempurl[0] as $value) 
  28.  { 
  29.   if(strstr($value,'http') ) 
  30.   { 
  31.    if(stristr($value ,$localurl)) 
  32.    { 
  33.     continue; 
  34.    } 
  35.    else 
  36.    {     
  37.     preg_match_all("/hrefs*=s*(['"]?)(.*?)/1/is", $value, $vlink);     
  38.     $urls[] = $vlink[2]; 
  39.    } 
  40.   } 
  41.  } 
  42.  //print_r($urls);Vevb.com
  43.  $strurl = explode('|',deleteemptyarray( $urls)); 
  44.  $tempcount = array_unique($strurl); 
  45.  echo '&nbsp;外链'.count($tempcount)-1; 
  46. } 

调用方法,代码如下:

  1. $body ="<a href=/ab.htm>aaa</a><a href=http://www.Vevb.com>我是外部连接</a>"; 
  2. echo getoutlink($body);

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表