- 论坛徽章:
- 0
|
1.12统计字符串里单词出现次数
?php
//1.12统计字符串里单词出现次数
function full_count_words($str) {
//返回完整数组,包含字符串里每个单词
$words = str_word_count($str,1);
$result = array();
foreach ($words as $w) {
$lw = strtolower($w);
//判断单词是否是第一次出现,是则设置为1,否则就增加1
if (!(isset($result[$lw]))) {
$result[$lw] = 1;
} else {
$result[$lw]++;
}
}
return $result;
}
$test = "Good luck to you,good by! me to ,good ,good";
$wordcount = full_count_words($test);
//echo $wordcount['good'];
print_r($wordcount);
/* 输出结果:
Array
(
[good] => 4
[luck] => 1
[to] => 2
[you] => 1
[by] => 1
[me] => 1
)
*/
?>
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/51625/showart_415568.html |
|