2011-08-14 5 views
1

아래 함수에서 제외 할 도메인 목록을 지정하고 싶습니다. 몇 가지 옵션은 무엇입니까? 제외 할 배열 컬렉션?경로 링크에서 URL을 제외 하시겠습니까?

http://baz.example.com 

는 노드 세트를 테스트 할 Xpath 1.0. specification for other possible string functions를 참조하십시오 :이 XPath를 태그하기 때문에

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

+1 멋진 제이슨이 보인다. 감사! –

+0

@Scott B 도움이 되었기 때문에 기꺼이 도와 드리겠습니다. 유용하다고 생각하시면 받아들이십시오. –

+0

오류가 발생합니다> "두 번째 인수 인 'myFilter'는 유효한 콜백이어야합니다." –

1

, 여기 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; 
} 

이가 출력을 수행하는 방법이다 .

관련 문제