我正在尝试获取一个字符串中3个单词的词频。例如:
$string =“这是一个示例文本。它是用作示例的示例文本。”;
我想要输出:
“是样本”(2)
“样本文本”(2)
……诸若此类
提前谢谢。
发布于 2014-02-07 18:26:48
使用空格作为分隔符拆分句子,然后循环执行。
<?php
$string = "This is a sample text. It is a sample text used as an example.";
$arr = explode(' ',$string);
$i=0;
foreach($arr as $k=>$v)
{
echo $arr[$i]." ".$arr[$i+1]." ".$arr[$i+2]."<br>";
$i++;
}OUTPUT :
This is a
is a sample
a sample text.
sample text. It
text. It is
It is a
is a sample
a sample text
sample text used
.....发布于 2014-02-07 19:19:52
$string = "This is a sample text. It is a sample text used as an example.";
$words = preg_split('/\W/', $string);
$wordsPerRow = 3;
$offset = 0;
while ($offset < count($words)) {
print implode(' ', array_slice($words, $offset, $wordsPerRow)).PHP_EOL;
$offset += $wordsPerRow;
}https://stackoverflow.com/questions/21625062
复制相似问题