2013-05-05 81 views
0

举例来说,如果我有这样的文章(串):什么功能

...

漆黑相当不像目前在Linux上可用的任何其他电子邮件应用程序 - 不仅仅在外观上,而且在功能上。

例如,漆黑扫描在设置过程中您的收件箱和通讯录工作 哪些消息更可能是“重要”给你,并 都没有。一条消息旁边的墨滴越黑越重要 Inky认为它。

...

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

我想数一个特殊的词。所以,结果是:

字:

漆黑(3个字)

电子邮件(1个字)

的Linux(1个字)

...

etc

我应该使用什么样的功能?

+0

想要对包含在字符串或文件中的单词进行计数吗?或者...?另外,你的意思是“一个特定的词”?你只想计算一个传入的单词作为参数的发生? – 2013-05-05 06:43:52

回答

1
<?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']; 
?> 

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

+1

+1不知道str_word_count() – 2013-05-05 07:09:55

+0

Thankyou上的格式参数。有用! :) – bob 2013-05-05 07:12:52

+0

是的......我没有使用过很多,但显然有些人(如现在)......)@bob:我也改善了答案,因为我认为有时候大写可能会有这是一个问题,所以它需要更少的编码(如果你还没有看到变化)。 – jerdiggity 2013-05-05 07:14:37