php正则表达式匹配友情链接(含批量网址+域名)
今天由于工作需要,辛苦了半天才整体完成了php正则表达式匹配友情链接,其中当然也有匹配所有网址和域名的功能,不错哦,值得推荐。
下面附上源代码:
$domain=”tech.sina.com.cn”;//域名变量,随便指定即可
$content=curl_file_get_contents(“http://www.”.$domain);
preg_match_all(‘/href=[\’|”]?([^ \'”>]*)[\’|”]?/’,$content,$alllinks);
for($i=0;$i<count($alllinks[0]);$i++){
$temp_str=preg_replace(“/\”/i”,””,$alllinks[0][$i]);
$temp_str=preg_replace(“/’/i”,””,$temp_str);
if(substr($temp_str,5,1)!=”/” && !strpos(“*”.$temp_str,$value)){
preg_match_all(‘~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?~i’,substr($temp_str,5),$urlarray);
if($urlarray[4][0]) echo $urlarray[4][0].”
“;
}
}
匹配网址正则(不会误伤email):
(?:[a-z0-9!#$%&’*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&’*+/=?^_`{|}~-]+)*
| “(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]
| \\[\x01-\x09\x0b\x0c\x0e-\x7f])*”)
@ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
| \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]
| \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)
\])
近期评论