Php 查找字符串中第一个重复的单词

Php 查找字符串中第一个重复的单词,php,Php,我需要从下面得到第一个重复单词的输出 $input ="we are the people who are selected by the people of country who gave vote." 所需的输出“是”您可以尝试使用和explode()使用分隔符(此处为空格)将字符串分解为数组,并array\u count\u values()计算数组元素的出现次数 $input ="we are the people who are selected by the people of

我需要从下面得到第一个重复单词的输出

$input ="we are the people who are selected by the people of country who gave vote."
所需的输出
“是”

您可以尝试使用和
explode()
使用分隔符(此处为空格)将字符串分解为数组,并
array\u count\u values()
计算数组元素的出现次数

$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;
工作


也可以看到。

欢迎,为了提高您的体验,请阅读一篇文章,以及如何创建专业提示:欢迎初学者,但我们希望您在提出问题之前花一些精力尝试解决自己的问题。我们不为您编写代码,尽管我们非常愿意用您编写的代码帮助您解决问题。介意与我们分享您的工作吗?我们很乐意提供帮助,但我们不是一个代码编写服务。str_word_count是一个很好的选择