PHP统计字符串中单词出现次数的函数
本节内容:
统计字符串里单词出现次数的函数
例子:
<?
/**
* 统计字符串里单词出现次数
* edit: www.jbxue.com
*/
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);
?>
您可能感兴趣的文章:
php 统计字数(支持中英文)的实现代码
PHP统计字符串中单词出现次数的函数
php5 字符串处理函数汇总
php字符串函数有哪些
php str_word_count函数怎么用
【PHP学习】新手必备PHP常用函数大集合
seo搜索引擎关键词技术
php字符串函数的简单示例代码
如何安装 php scws(分词组件)?
php计算未知长度的字符串中出现的次数最多字符的代码