3

我正在尝试使用 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 文件中删除不需要的 url 的最佳方法吗?:)

4

2 回答 2

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>with进行粗略匹配href=,抓取引号之间的内容,然后根据您的域白名单对其进行过滤。

于 2013-09-25T21:04:55.360 回答
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
)
于 2013-09-25T21:18:19.110 回答