从路径链接中排除url


excluding URLs from path links?

在下面的函数中,我想指定要从结果中排除的域列表。有哪些选择?要排除的数组集合?

class KeywordSearch
{       
    const GOOGLE_SEARCH_XPATH = "//a[@class='l']";
    public $searchQuery;
    public $numResults ;
    public $sites;
    public $finalPlainText = '';
    public $finalWordList = array();
    public $finalKeywordList = array();
    function __construct($query,$numres=7){
        $this->searchQuery = $query;
        $this->numResults = $numres;
        $this->sites = array();
    }
    protected static $_excludeUrls  = array('wikipedia.com','amazon.com','youtube.com','zappos.com');//JSB NEW
    private function getResults($searchHtml){
        $results = array();
        $dom = new DOMDocument();
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = false;
        @$dom->loadHTML($searchHtml);
        $xpath = new DOMXpath($dom);
        $links = $xpath->query(self::GOOGLE_SEARCH_XPATH);
        foreach($links as $link)
        {
            $results[] = $link->getAttribute('href');           
        }
        $results = array_filter($results,'self::kwFilter');//JSB NEW
        return $results;
    }
    protected static function kwFilter($value)
    {
        return !in_array($value,self::$_excludeUrls);
    }   
protected static $_banUrls  = array('foo.com','bar.com');
private function getResults($searchHtml){
        $results = array();
        $dom = new DOMDocument();
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = false;
        @$dom->loadHTML($searchHtml);
        $xpath = new DOMXpath($dom);
        $links = $xpath->query(self::GOOGLE_SEARCH_XPATH);

        foreach($links as $link)
        {
        //FILTER OUT SPECIFIC LINKS HERE
            $results[] = $link->getAttribute('href');
        }
        $results = array_filter($results,'self::myFilter');
        return $results;
    }
    protected static function myFilter($value)
    {
            return !in_array($value,self::$_banUrls);
    }

既然您标记了这个XPath,下面是如何使用XPath contain函数:

$html = <<< HTML
<ul>
    <li><a href="http://foo.example.com">
    <li><a href="http://bar.example.com">
    <li><a href="http://baz.example.com">
</ul>
HTML;
$dom = new DOMDocument;
$dom->loadHtml($html);
$xp = new DOMXPath($dom);
$query = '//a/@href[
    not(contains(., "foo.example.com")) and
    not(contains(., "bar.example.com"))
]';
foreach ($xp->query($query) as $hrefAttr) {
    echo $hrefAttr->nodeValue;
}

这将输出:

http://baz.example.com

参见Xpath 1.0。用于测试节点集的其他可能的字符串函数规范。