PHP Regex或DOMDocument for Matching&删除网址?

时间:2013-09-25 20:36:46

标签: php regex domdocument

我正在尝试使用DOM从html页面中提取链接:

$html = file_get_contents('links.html');
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$a = $DOM->getElementsByTagName('a');
foreach($a as $link){
    //echo out the href attribute of the <A> tag.
    echo $link->getAttribute('href').'<br/>';
}

输出:

http://dontwantthisdomain.com/dont-want-this-domain-name/
http://dontwantthisdomain2.com/also-dont-want-any-pages-from-this-domain/
http://dontwantthisdomain3.com/dont-want-any-pages-from-this-domain/
http://domain1.com/page-X-on-domain-com.html

http://dontwantthisdomain.com/dont-want-link-from-this-domain-name.html
http://dontwantthisdomain2.com/dont-want-any-pages-from-this-domain/
http://domain.com/page-XZ-on-domain-com.html

http://dontwantthisdomain.com/another-page-from-same-domain-that-i-dont-want-to-be-included/
http://dontwantthisdomain2.com/same-as-above/
http://domain3.com/page-XYZ-on-domain3-com.html

我想删除与dontwantthisdomain.com,dontwantthisdomain2.com和dontwantthisdomain3.com匹配的所有结果,以便输出看起来像这样:

http://domain1.com/page-X-on-domain-com.html
http://domain.com/page-XZ-on-domain-com.html
http://domain3.com/page-XYZ-on-domain3-com.html

有些人说我不应该为html和其他人使用正则表达式,这是可以的。有人可以指出如何从我的html文件中删除不需要的网址的最佳方法吗? :)

2 个答案:

答案 0 :(得分:2)

也许是这样的:

function extract_domains($buffer, $whitelist) {
    preg_match_all("#<a\s+.*?href=\"(.+?)\".*?>(.+?)</a>#i", $buffer, $matches);
    $result = array();
    foreach($matches[1] as $url) {
        $url = urldecode($url);
        $parts = @parse_url((string) $url);
        if ($parts !== false && in_array($parts['host'], $whitelist)) {
            $result[] = $parts['host'];
        }
    }
    return $result;
}

$domains = extract_domains(file_get_contents("/path/to/html.htm"), array('stackoverflow.com', 'google.com', 'sub.example.com')));

它与所有<a>href=进行粗略匹配,抓取引号之间的内容,然后根据您的域名白名单对其进行过滤。

答案 1 :(得分:1)

无正则表达式解决方案(没有潜在错误:-):

$html='
http://dontwantthisdomain.com/dont-want-this-domain-name/
http://dontwantthisdomain2.com/also-dont-want-any-pages-from-this-domain/
http://dontwantthisdomain3.com/dont-want-any-pages-from-this-domain/
http://domain1.com/page-X-on-domain-com.html

http://dontwantthisdomain.com/dont-want-link-from-this-domain-name.html
http://dontwantthisdomain2.com/dont-want-any-pages-from-this-domain/
http://domain.com/page-XZ-on-domain-com.html

http://dontwantthisdomain.com/another-page-from-same-domain-that-i-dont-want-to-be-included/
http://dontwantthisdomain2.com/same-as-above/
http://domain3.com/page-XYZ-on-domain3-com.html
';

$html=explode("\n", $html);
$dontWant=array('dontwantthisdomain.com','dontwantthisdomain2.com','dontwantthisdomain3.com');
foreach ($html as $link) {
    $ok=true;
    foreach($dontWant as $notWanted) {
        if (strpos($link, $notWanted)>0) { 
            $ok=false;
        }
        if (trim($link=='')) $ok=false;
    }
    if ($ok) $final_result[]=$link;
}

echo '<pre>';
print_r($final_result);
echo '</pre>';

输出

Array
(
    [0] => http://domain1.com/page-X-on-domain-com.html
    [1] => http://domain.com/page-XZ-on-domain-com.html
    [2] => http://domain3.com/page-XYZ-on-domain3-com.html
)