这是我使用 preg_match 编写的一个小示例。让我知道它是否有帮助。
<?php
function get_a_href($url){
$url = htmlentities(strip_tags($url));
$ExplodeUrlInArray = explode('/',$url);
$DomainName = $ExplodeUrlInArray[2];
$file = @file_get_contents($url);
$h1count = preg_match_all('/(href=["|\'])(.*?)(["|\'])/i',$file,$patterns);
$linksInArray = $patterns[2];
$CountOfLinks = count($linksInArray);
$InternalLinkCount = 0;
$ExternalLinkCount = 0;
for($Counter=0;$Counter<$CountOfLinks;$Counter++){
if($linksInArray[$Counter] == "" || $linksInArray[$Counter] == "#")
continue;
preg_match('/javascript:/', $linksInArray[$Counter],$CheckJavascriptLink);
if($CheckJavascriptLink != NULL)
continue;
$Link = $linksInArray[$Counter];
preg_match('/\?/', $linksInArray[$Counter],$CheckForArgumentsInUrl);
if($CheckForArgumentsInUrl != NULL)
{
$ExplodeLink = explode('?',$linksInArray[$Counter]);
$Link = $ExplodeLink[0];
}
preg_match('/'.$DomainName.'/',$Link,$Check);
if($Check == NULL)
{
preg_match('/http:\/\//',$Link,$ExternalLinkCheck);
if($ExternalLinkCheck == NULL)
{
$InternalDomainsInArray[$InternalLinkCount] = $Link;
$InternalLinkCount++;
}
else
{
$ExternalDomainsInArray[$ExternalLinkCount] = $Link;
$ExternalLinkCount++;
}
}
else
{
$InternalDomainsInArray[$InternalLinkCount] = $Link;
$InternalLinkCount++;
}
}
$LinksResultsInArray = array(
'ExternalLinks'=>$ExternalDomainsInArray,
'InternalLinks'=>$InternalDomainsInArray
);
return $LinksResultsInArray;
}
?>