簡體   English   中英

從數組中排除某些單詞

[英]Excluding certain words from an array

下表顯示$commentstring字符串中的所有單詞。 如何排除某些冠詞,介詞和動詞,例如“ the,of,is”?

$words = explode(" ", $commentstring);

$result = array();
arsort($words);

foreach($words as $word) {    
  if(!is_numeric($word)){
    $result[$word]++;
    arsort($result);
  }
}

echo "<table>";

foreach($result as $word => $count1) {
  echo '<tr>';  
  echo '<td>';
  echo "$word";
  echo '</td>';

  echo '<td>';
  echo "$count1 ";
  echo '</td>';

  echo '</tr>';
}

echo "</table>";

有幾種方法可以執行此操作,如果您仍然希望對它們進行計數,但又不想在表中顯示它們,則可以執行以下操作:

$blacklist = array('the', 'is', 'a');

foreach($result as $word => $count1)
{
    if (in_array($word, $blacklist)) continue;

    ...

如果您甚至不想對它們進行計數,都可以在計數循環中以類似的方式跳過它們。

暫無
暫無

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

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