使数组不区分大小写

时间:2012-02-04 02:18:00

标签: php

下面的代码会返回一个表格,其中包含$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>";

2 个答案:

答案 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>";