从数组中排除某些单词


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;
    ...

如果你甚至不想计数,你可以在计数循环中以类似的方式跳过它们。