复制代码 代码如下:
function match_links($document) {
preg_match_all(“‘<s*as.*?hrefs*=s*([“‘])?(?(1)(.*?)\1|([^s>]+))[^>]*>?(.*?)</a>’isx”,$document,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val))
$match[‘link’][] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match[‘link’][] = $val;
}
while(list($key,$val) = each($links[4])) {
if(!empty($val))
$match[‘content’][] = $val;
}
while(list($key,$val) = each($links[0])) {
if(!empty($val))
$match[‘all’][] = $val;
}
return $match;
}
preg_match_all(“‘<s*as.*?hrefs*=s*([“‘])?(?(1)(.*?)\1|([^s>]+))[^>]*>?(.*?)</a>’isx”,$document,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val))
$match[‘link’][] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match[‘link’][] = $val;
}
while(list($key,$val) = each($links[4])) {
if(!empty($val))
$match[‘content’][] = $val;
}
while(list($key,$val) = each($links[0])) {
if(!empty($val))
$match[‘all’][] = $val;
}
return $match;
}
主要是正则的问题,下面给出个asp.net下的,多测试正则
获取页面的链接正则
复制代码 代码如下:
public string GetHref(string HtmlCode)
{
string MatchVale = “”;
string Reg = @”(h|H)(r|R)(e|E)(f|F) *= *(‘|””)?((w|\|/|.|:|-|_)+)(‘|””| *|>)?”;
foreach (Match m in Regex.Matches(HtmlCode, Reg))
{
MatchVale += (m.Value).ToLower().Replace(“href=”, “”).Trim() + “||”;
}
return MatchVale;
}
{
string MatchVale = “”;
string Reg = @”(h|H)(r|R)(e|E)(f|F) *= *(‘|””)?((w|\|/|.|:|-|_)+)(‘|””| *|>)?”;
foreach (Match m in Regex.Matches(HtmlCode, Reg))
{
MatchVale += (m.Value).ToLower().Replace(“href=”, “”).Trim() + “||”;
}
return MatchVale;
}