如果我想要计算我在一篇文章中找到的特定单词,那该怎么办呢?



例如,如果我有这篇文章(string):

Inky与目前Linux上可用的任何其他电子邮件应用程序都非常不同-不仅在外观上,而且在功能上。

例如,Inky扫描你的收件箱和联系人在设置工作找出哪些信息对你来说更"重要",哪些更重要不是。信息旁边墨滴的颜色越深,说明信息越重要Inky认为。

[来源:http://www.omgubuntu.co.uk/2013/05/inky-pens-linux-support-on-roadmap]

我想数一个特定的单词。因此,结果是:

词:

墨迹(3字)

email(1字)

Linux(1字)

…等

我应该在php中使用什么函数?

<?php
  $string = <<<_STRING_
  Inky is pretty unlike any other email app currently available on Linux – not just in looks but also in features.
  For example, Inky scans your inbox and contacts during set-up to work out which messages are more likely to be ‘important’ to you, and which aren’t. The darker an ink drop next to a message the more important Inky considers it.
_STRING_;
$word_count = str_word_count($string, 1);
$search_for = array('Inky', 'linux', 'email');
foreach ($search_for as $item) {
  $count[$item] = 0;
}
foreach ($word_count as $key => $word) {
  if (in_array($word, $search_for)) {
    $count[$word]++;
  }
}
print $count['Inky'];
print $count['linux'];
print $count['email'];
?>

只是一个粗略的例子,但希望它能让你走上正确的道路。

相关内容

最新更新