1
在下面的函數中,我想指定一個從結果中排除的域列表。有什麼選擇?數組集合要排除?排除路徑鏈接的網址?
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看起來不錯Jason。謝謝! –
@Scott B很高興它幫助你,如果你覺得它有用,請接受它。 –
我收到一個錯誤>「第二個參數'myFilter'應該是一個有效的回調」 –