我正在尝试使用DOM从HTML页面中提取链接:
我希望删除所有与dontwantthisdomain.com、dontwantthisdomain2.com和dontwantthisdomain3.com匹配的结果,以便输出看起来像这样:
$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? :)
parse_url
获取域名(host),然后仅检查它是否在不需要的域名黑名单中。 - Martin Ender