PHP Regex 或 DOMDocument 用于匹配和删除 URL


PHP Regex or DOMDocument for Matching & Removing URLs?

我正在尝试使用 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 的最佳方法吗?:)

也许是这样的:

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

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

$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
)