1

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);
    }   
Scott B
  • 38,833
  • 65
  • 160
  • 266

2 Answers2

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);
    }
Mr Coder
  • 8,169
  • 5
  • 45
  • 74
  • @Scott B glad it helped you , do accept it if you find it useful thx. – Mr Coder Aug 14 '11 at 16:35
  • I'm getting an error > "The second argument, 'myFilter', should be a valid callback" – Scott B Aug 14 '11 at 16:36
  • This is all in a class if that makes a difference. – Scott B Aug 14 '11 at 16:38
  • still no joy. I've pasted the revised code into the original question. Error > "Fatal error: Cannot call method self::kwFilter() or method does not exist" – Scott B Aug 14 '11 at 17:27
  • @Scott B wats kwFilter() ?? i think you have forgot to change the name of function to myFilter() . – Mr Coder Aug 14 '11 at 17:31
  • @ScottB let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2467/discussion-between-jason-bourne-and-scott-b) – Mr Coder Aug 14 '11 at 17:31
1

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559