在字符串中出现最多的Php单词

在字符串中出现最多的Php单词,php,Php,我应该在句子中找到出现最多的单词 这是我尝试过的,但不起作用 <?php $str = "Hello friend, Hello good good today!"; $time=array(); $cnt=str_word_count($str, 1); $times=reset($cnt); $count=0; foreach($cnt as $val){ $times=$val; foreach($cnt as $val){ if($time

我应该在句子中找到出现最多的单词

这是我尝试过的,但不起作用

<?php
$str = "Hello friend, Hello good  good today!";
$time=array();
$cnt=str_word_count($str, 1);
$times=reset($cnt);
$count=0;
foreach($cnt as $val){
    $times=$val;    
    foreach($cnt as $val){
        if($times===$val){
            $count++;
            $times=$times . $count;     
        }       
    }
    $count=0;
}
print_r($cnt);
print_r($times);    

这是我的看法。这有点长,因为我包括了所有的评论并解释了一切:

清理字符串是可选的,但强烈建议。它可以防止像“今天”和“今天”这样的故障


尝试使用我上面的代码。希望它能起作用。如果最经常出现的单词只有一个单词,则将显示该单词以及该单词的出现次数。如果有多个单词,则只要出现次数相同,就会显示所有这些单词。

使用:

<?php

$str = "Hello friend, Hello good  good today!";

//$str = strtolower($str); // use this line for case insensitive words

$cnt=str_word_count($str, 1);

$word_counts = array_count_values($cnt);

arsort($word_counts);

$max_val = 0; 
$max_appears = array();
foreach($word_counts as $key=>$value) {
    if($value>=$max_val) {
        $max_appears[$key] = $value;
        $max_val = $value;  
    }
}
print_r($max_appears);

我不明白为什么否决这个?@Mr.Alien:这就是为什么^(虽然不是我)@MadaraUchiha哈哈我意识到了,所以没有回答;)@外星人先生:我同意。巨魔将是巨魔…@Yogeshuthar Yea^^^和estra->谢谢Yogeshuthar是的,我只是想知道为什么在第一个脚本中你没有使用数组计数值而不是整个foreach循环。很高兴你在第二个脚本中完成了。非常有用!谢谢!当你有两个词出现相同次数时,你会怎么做?谢谢!非常有用。你也可以检查不区分大小写的单词。我知道询问者的反馈请求
$str = "Hello friend, Hello good  good today!";

//Import words into array
$words = str_word_count($str, 1);

//Count same values
$count = array_count_values($words);

//Ascending sort
arsort($count);

var_dump($count);
<?php
$str = "Hello friend, Hello good  good today!";
$count = array();
$words = array();
$cnt = str_word_count($str, 1);
foreach ($cnt as $val){
    $i = 0;
    $term = false;
    foreach ($words as $word){
        if ($word === $val){
            $count[$i]++;
            $term = true;
            break;
        }
        $i++;
    }
    if ($term)
        continue;
    $words[$i] = $val;
    $count[$i] = 1;
}
print_r($cnt);
print_r('<br/>');
$max = -1;
$resultWords = array();
$resultCount = array();
$i = 0;
foreach ($count as $c){
    if ($max == $c){
        $resultWords[] = $words[$i];
        $resultCount[] = $c;
    }else if ($max < $c){
        $resultWords = array();
        $resultCount = array();
        $resultWords[] = $words[$i];
        $resultCount[] = $c;
        $max = $c;
    }
    $i++;
}
foreach ($resultWords as $result){
    print_r($result.' '.$max.'<br/>');
}
?>
<?php

$str = "Hello friend, Hello good  good today!";

//$str = strtolower($str); // use this line for case insensitive words

$cnt=str_word_count($str, 1);

$word_counts = array_count_values($cnt);

arsort($word_counts);

$max_val = 0; 
$max_appears = array();
foreach($word_counts as $key=>$value) {
    if($value>=$max_val) {
        $max_appears[$key] = $value;
        $max_val = $value;  
    }
}
print_r($max_appears);
foreach($max_appears as $key=>$value) {
    echo $key." appears ".$value." times<br>";
}