下面的代码会返回一个表格,其中包含$commentstring
中显示的每个字词或数字的行。每个单词或数字在下表中显示为$word
。这是区分大小写的。如何使其不区分大小写呢?
$words = explode(" ", $commentstring);
$result = array();
arsort($words);
foreach($words as $word) {
if(!is_numeric($word)){
$result[$word]++;
arsort($result);
}
}
echo "<table>";
$blacklist = array('the', 'is', 'a');
foreach($result as $word => $count1)
{
if (in_array($word, $blacklist)) continue;
echo '<tr>';
echo '<td>';
echo "$word";
echo '</td>';
echo '<td>';
echo "$count1 ";
echo '</td>';
echo '</tr>';
}
echo "</table>";
答案 0 :(得分:1)
变化:
if (in_array($word, $blacklist)) continue;
为:
if (in_array(strtolower($word), $blacklist)) continue;
答案 1 :(得分:0)
在这里,这将完全符合您的要求,并且当我更改in_array
以使用array_flip
&amp; isset
诀窍:
$words = explode(' ', $commentstring);
$result = array();
arsort($words);
foreach($words as $word) {
if(!is_numeric($word)){
$result[$word]++;
arsort($result);
}
}
echo "<table>";
$blacklist = 'the is a';
$blacklist = explode(' ', strtolower($blacklist));
$blacklist = array_flip($blacklist);
foreach($result as $word => $count1)
{
if (isset($blacklist[strtolower($word)])) continue;
echo '<tr>';
echo '<td>';
echo "$word";
echo '</td>';
echo '<td>';
echo "$count1 ";
echo '</td>';
echo '</tr>';
}
echo "</table>";