簡體   English   中英

遍歷div並使用simplehtmldom提取文本

[英]loop through divs and extract text using simplehtmldom

我正在使用simplehtmldom從網站上獲取html。 然后,我搜索頁面上的所有div,並顯示單詞數大於300的內部文本。為此,我使用foreach進行迭代。

$findDivs = $html->find('div');

foreach($findDivs as $findDiv) {
  $wordCount = explode(' ', $findDiv->outertext);
  $wordCount = count($wordCount);
  if($wordCount <= 300) {
    $findDiv->outertext = '';
   }
   else {
     echo $findDiv->outertext . '<br />';
  }
}

我的問題是結果重復了6次。 我只能假定這是因為所有div都在每次迭代中都經過了循環。 但是,我不確定我可以使用什么技術來確保每個div僅評估一次。

您需要innertext但是您的代碼聲明了outertext -我認為這是重復的原因。

foreach($html->find('div') as $findDiv) {
  $wordCount = explode(' ', $findDiv->innertext);
  $wordCount = count($wordCount);
  if($wordCount > 300) {
    echo $findDiv->outertext . '<br />';
   }
}

我不確定為什么,但是這解決了我的問題。

我在$ html-> find('div',1);中添加了'1'參數;

因此,工作代碼如下所示:

$findDivs = $html->find('div',1);  //add a 1 to the divs. this works as the script now only loops once.

foreach($findDivs as $findDiv) {
  $wordCount = explode(' ', $findDiv->outertext);
  $wordCount = count($wordCount);
  if($wordCount <= 300) {
    $findDiv->outertext = '';
   }
   else {
     echo $findDiv->outertext . '<br />';
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM