Ok, so as I'm getting my hands dirty with PHP I've started playing around with arrays, strings and the like.
Now I know that arrays have a neat feature called "array_count_values" which can help to determine what the most repeated entry is. I wasn't able to find a string equivalent for this- will I need to convert the string to an array?
Basically, I want my code to determine what the most common (repeated) word is over a certain length in a given string.
Sans the character length qualification, this code can find the answer to the question of what is the most repeated word in an array:
<?php
$param[0]="Ted";
$param[1]="Mark";
$param[2]="Mark";
$param[3]="Ross";
$param[3]="Clarence";
function array_most_common($arr)
{
$counted = array_count_values($arr);
arsort($counted);
return(key($counted));
}
$mostCommon = array_most_common($param);
echo $mostCommon;
?>
So what would make this doable with a string? And a character amount filter?
With a string, you can just explode()
or preg_split()
on space to form an array. Using preg_split()
is favorable as it will eliminate duplicate and extraneous whitespace that explode()
will not.
$array = preg_split( '/\s+/', "This is a pretty long long long string", -1, PREG_SPLIT_NO_EMPTY);
Then, once you have an array, use array_filter()
to remove those that don't meet the character requirements:
$threshold = 3;
$filtered = array_filter( $array, function( $el) use( $threshold) {
return strlen( $el) > $threshold;
});
Once you have the $filtered
array, just use that in array_count_values()
.
$counts = array_count_values( $filtered);
arsort( $counts);
echo key( $counts) . ' -> ' . current( $counts);
Here is a demo, which prints:
long -> 3
To answer your question, there is not a function for determining the most common word in a string as far as I'm aware. However, you can explode()
the string by space, and array_count_values()
the resulting array instead. I'm not too certain what you mean by a "character amount filter" or where you plan to implement this.
$str = strtolower("The quick brown fox jumps over the lazy dog");
$words = explode(" ", $str);
$words = array_filter($words, function($word) {
return strlen($word) > 2;
});
$word_counts = array_count_values($words);
arsort($word_counts);
$most_common_word = key($word_counts); // Returns "the"