PHP:3个最常见的单词 [英] PHP: 3 Most Common Words
本文介绍了PHP:3个最常见的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我正在进行的课程作业,而不是完全理解。只是希望在正确的方向上提供一些帮助。
Directons:
参数:包含在左边用空格分隔的单词的字符串在右边有空格,逗号,句号或问号
返回值:字符串中三个最常见的单词,超过
<?php
功能拆分器($ str){
$ freq = array();
$ words = preg_split( / [\。,;:!\ /] \ * * /,$ str);
foreach ($ words as $ word){
< span class =code-sdkkeyword> $ keys = array_keys($ freq);
if(in_array($ word,$ keys))
$ freq [$ word] ++;
else
$ freq [$ word] = 1 ;
}
return $ freq ;
}
$ str = 我喜欢跑步或慢跑锻炼。然后,我喜欢喝大量的液体。;
$ result = splitter($ str);
sort($ result);
foreach ($ result as $ word){
$ len = strlen($ word);
if ($ len> 3)
print $字跨度>;
else
return false;
}
?>
此代码不返回任何内容。我已尝试了一些变化而且此时丢失了。
解决方案
str){
freq = array();
words = preg_split( / [\。,;:!\ /] \s * /,
This is a class assignment that I am working on and not understanding completely. Just would like some help in the right direction.
Directons:
Parameter: A string containing words that are delimited on the left with spaces and on the right with spaces, commas, periods, or question marks
Return value: The three most common words in the string that have more than
<?php
function splitter($str) {
$freq = array();
$words = preg_split("/[\.,;:!\/]\s*/", $str);
foreach ($words as $word) {
$keys = array_keys($freq);
if(in_array($word, $keys))
$freq[$word]++;
else
$freq[$word] = 1;
}
return $freq;
}
$str = "I like to run or jog for exercise. Then, I like to drink plenty of fluids.";
$result = splitter($str);
sort($result);
foreach ($result as $word) {
$len = strlen($word);
if ($len > 3)
print $word;
else
return false;
}
?>
This code returns nothing. I have tried a few variations and just lost at this point.
解决方案
str) {
freq = array();
words = preg_split("/[\.,;:!\/]\s*/",
这篇关于PHP:3个最常见的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文