我正在对一些文本进行排序,我想要统计某个单词出现的次数。php长度或计数字符串
像
文字部分我不想法,你有一些想法,我们将制定出一些 想法,我也没想法,你有一些想法,我们将制定一些想法,我没有想法,你有一些想法,我们将努力 一些想法
我如何使用PHP来算多少次“想法”发生,就像词“想法”发生 我在数量和长度困惑。
有人可以举个例子吗?非常感谢
我正在对一些文本进行排序,我想要统计某个单词出现的次数。php长度或计数字符串
像
文字部分我不想法,你有一些想法,我们将制定出一些 想法,我也没想法,你有一些想法,我们将制定一些想法,我没有想法,你有一些想法,我们将努力 一些想法
我如何使用PHP来算多少次“想法”发生,就像词“想法”发生 我在数量和长度困惑。
有人可以举个例子吗?非常感谢
请尝试下面的脚本。 str_word_count()会将句子拆分为单词数组,array_count_values()将分配每个单词出现的次数。
$words = str_word_count($text, 1);
$times = array_count_values($words);
echo $times['idea'];
Substr_count是你的朋友。 http://php.net/manual/en/function.substr-count.php
使用像搜索突出法(修改,以自己的方式工作)
<?php
$s = 'idea';
$text = "I have no idea, you got some idea, we will work out some idea, I have no idea, you got some idea, we will work out some idea, I have no idea,you got some idea, we will work out some idea";
$keys = explode(" ",$s);
$count = preg_match_all('/('. implode('|', $keys) .')/iu', $text);
echo $count;
?>
相较于substr_count功能,这会算 'ideaidea' 两个。
你需要数_words_或_substrings_吗?例如,“理想主义”中的“观念”也应该计算在内吗?如果'是',请在'substr_count'之间选择提供答案。 )否则,请考虑使用'str_word_count'或其正则表达式。 – raina77ow