php教程 正則字符串中外部鏈接代碼函數
function deleteemptyarray( $val )
{
$links ='';
if( is_array( $val ) )
{
foreach( $val as $v =>$_v)
{
if( !empty( $_v[0] ) )
{
$links .=$_v[0].'|';
}
}
return substr($links,0,-1);
}
else
{
return false;
}
}function getoutlink($body)
{
$tempcontent = $body;
preg_match_all("/<a(.*?)href=(.*?)</a>/i",$tempcontent,$tempurl);
$urls =array();
foreach($tempurl[0] as $value)
{
if(strstr($value,'http') )
{
if(stristr($value ,$localurl))
{
continue;
}
else
{
preg_match_all("/hrefs*=s*(['"]?)(.*?)1/is", $value, $vlink);
$urls[] = $vlink[2];
}
}
}
//print_r($urls);
$strurl = explode('|',deleteemptyarray( $urls));
$tempcount = array_unique($strurl);
echo ' 外鏈'.count($tempcount)-1;
}
//調用方法
$body ="<a href=/ab.htm>aaa</a><a href=http://www.bkjia.com>我是外部連接</a>";
echo getoutlink($body);