I need to get the output of the first repeated word from the following
$input ="we are the people who are selected by the people of country who gave vote."
Desired output "are"
I need to get the output of the first repeated word from the following
$input ="we are the people who are selected by the people of country who gave vote."
Desired output "are"
You can try with explode() and array_count_values(). explode()
break down the string to array by a delimiter (here space) and array_count_values()
count the occurrence of the array elements.
$input ="we are the people who are selected by the people of country who gave vote.";
$words = array_count_values(explode(' ', strtolower($input)));
$first_occ = '';
foreach ($words as $word => $count) {
if ($count > 1) {
$first_occ = $word;
break;
}
}
echo $first_occ;
Working demo.
Also can see str_word_count.