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

时间:2013-09-07 07:17:08

标签: 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($times===$val){
            $count++;
            $times=$times . $count;     
        }       
    }
    $count=0;
}
print_r($cnt);
print_r($times);    

3 个答案:

答案 0 :(得分:4)

这是我的看法。这有点长,因为我收录了所有评论并解释了所有内容:

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

<?php
$str   = "Hello friend, Hello good  good today!";
$count = array();
/*
 * Remove all common special characters (so that "today" is equal to "today,"
 * Also lowercase the entire string, so that "Hello" is equal to "hello".
 */
$str = preg_replace("/[.,!?:(){}\[\]@#$%\^&\*\-_]/", " ", $str);
$str = strtolower($str);
/*
 * Split by spaces.
 * The reason I'm using preg_split instead of explode is because there can be multiple spaces in succession
 * And we don't want excess empty array elements.
 */
$words = preg_split("/\s+/", $str, -1, PREG_SPLIT_NO_EMPTY);

/*
 * Iterate the words...
 */
foreach ($words as $word) {
    /*
     * If this is the first time we encounter the word...
     */
    if (!isset($count[$word])) {
        /*
         * Set its count to one, and skip the rest of the loop
         */
        $count[$word] = 1;
        continue;
    }
    /*
     * Increase the count of the word by one (won't be reached if first encounter
     * Which means it would only happen if we already met the word.
     */
    $count[$word]++;
}
/*
 * Reverse sort with associative keys kept.
 */
arsort($count);

/*
 * Show me the money!
 */
var_dump($count);

较短的版本,使用PHP的本机函数:

$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);

答案 1 :(得分:0)

使用:

<?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>";
}

答案 2 :(得分:-1)

<?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/>');
}
?>

尝试使用上面的代码。希望它会奏效。如果最常见的单词只有一个单词,那么将显示该单词以及该单词的出现次数。如果有多个单词,则只要出现次数,所有单词都会显示。