在下面的函数中,我想指定要从结果中排除的域列表。有哪些选择?要排除的数组集合?
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);
}
答案 0 :(得分: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);
}
答案 1 :(得分:1)
由于您标记了此XPath,以下是使用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;
}
这将输出:
http://baz.example.com
请参阅Xpath 1.0. specification for other possible string functions以测试节点集。