2012-02-04 131 views
1

下表顯示字符串$commentstring中的所有單詞。我如何排除某些文章,介詞和動詞,如「是,是,是」?排除數組中的某些字

$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>"; 
+1

preg_replace怎麼樣? – ngen 2012-02-04 01:45:49

+0

http://stackoverflow.com/q/1728727/212218可能的重複 – 2012-02-04 01:50:58

回答

0

幾種方法可以做到這一點,如果你還想指望他們只是沒有在表格中顯示出來,你可以這樣做:

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

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

    ... 

,如果你甚至不想指望他們,您可以在計數循環中以類似的方式跳過它們。