excluding URLs from path links?

徘徊边缘 提交于 2019-12-24 07:21:25

问题


In the function below, I'd like to specify a list of domains to exclude from the results. What are some options? Array collection to exclude?

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);
    }   

回答1:


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);
    }



回答2:


Since you tagged this XPath, here is how to do it with XPath contain function:

$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;
}

This will output:

http://baz.example.com

See the Xpath 1.0. specification for other possible string functions to test node-sets.



来源:https://stackoverflow.com/questions/7057676/excluding-urls-from-path-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!