I'm supposed to find to word in sentence that appears the most.
This is what I've tried and it doesn't work.
<?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!";
$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/>');
}
?>
try using my code above. Hope it will work. If the most recurring word is only one word, then that word along with number of occurrence of that word will be display. If there is more than one word, all that words will be displayed as long as the number of occurrence.
Here's my take. It's a bit long because I included all comments and explained everything:
Sanitizing the string is optional, but highly recommended. It would prevent glitching like "today," vs "today".
<?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);
A shorter version, using PHP's native functions:
$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);
Use:
<?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);
Also you can print this array to string
foreach($max_appears as $key=>$value) {
echo $key." appears ".$value." times<br>";
}