繁体   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