Итак, вот функция, которую вы можете использовать, чтобы найти массив повторяющихся слов и ключей, и использовать ее для отображения дубликатов в цикле, если ключ совпадает в любом из подмассива, это дубликат.
$sentence = "Back in 1951 , the astronomer , Gerard P . Kuiper , was pondering the crowns , especially those that loop around the Sun in less than 200 years . These comets include Halley's Comet - last seen from earth in 1985/86 - and Shoemaker-Levy which crashed into Jupiter in 1994 . He worked out that comets like these would have to come from a belt fairly close to the planets of the solar system . He noticed that these comments also tend to come hurting in from outer space quite close to the plane of the planets , rather than from just any old direction . Kuiper predicted that there should be a flattened belt or frisk of comets and asteroids , beginning just outside the orbit of Neptune 30 AU and reaching out to about 1,000 AU or astronomical units . But finding a comet past Neptune , is like trying to see a 100-watt light bulb at 20 times the distance of the Moon . Well , back in 1992 , our television technology finally got good enough .";
$marked = explode(',', 'crowns,that,comments,frisk,finding');
$answers = explode(',', 'was,that,comments,see,finding');
$words = preg_split("/(\w\S+\w)|(\w+)|(\s*\.{3}\s*)|(\s*[^\w\s]\s*)|\s+/", $sentence,-1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE) ;
function find_duplicate_word_keys($words, $answers, $marked){
$search_words = $answers;
$duplicate_word_keys = [];
foreach($search_words as $search_word){
if(in_array($search_word, $marked)){
foreach($words as $key=>$word){
if($search_word == $word){
$duplicate_word_keys[$search_word][] = $key;
}
}
}
}
return $duplicate_word_keys;
}
$duplicate_word_keys = find_duplicate_word_keys($words, $answers, $marked);
print_r($duplicate_word_keys);
вот рабочая ссылка , надеюсь, это поможет.
Редактировать: Новый код
Пожалуйста, попробуйте.
$sentence = "Back in 1951 , the astronomer , Gerard P . Kuiper , was pondering the crowns , especially those that loop around the Sun in less than 200 years . These comets include Halley's Comet - last seen from earth in 1985/86 - and Shoemaker-Levy which crashed into Jupiter in 1994 . He worked out that comets like these would have to come from a belt fairly close to the planets of the solar system . He noticed that these comments also tend to come hurting in from outer space quite close to the plane of the planets , rather than from just any old direction . Kuiper predicted that there should be a flattened belt or frisk of comets and asteroids , beginning just outside the orbit of Neptune 30 AU and reaching out to about 1,000 AU or astronomical units . But finding a comet past Neptune , is like trying to see a 100-watt light bulb at 20 times the distance of the Moon . Well , back in 1992 , our television technology finally got good enough .";
$marked = explode(',', 'crowns,that,comments,frisk,finding');
$answers = explode(',', 'was,that,comments,see,finding');
$words = preg_split("/(\w\S+\w)|(\w+)|(\s*\.{3}\s*)|(\s*[^\w\s]\s*)|\s+/", $sentence,-1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE) ;
function find_duplicate_word_keys($words, $answers, $marked){
$search_words = $answers;
$duplicate_word_keys = [];
foreach($search_words as $search_word){
if(in_array($search_word, $marked)){
foreach($words as $key=>$word){
if($search_word == $word){
$duplicate_word_keys[$search_word][] = $key;
}
}
}
}
return $duplicate_word_keys;
}
$duplicate_word_keys = find_duplicate_word_keys($words, $answers, $marked);
// print_r($duplicate_word_keys);
$answer_keys = array_keys($duplicate_word_keys);
echo '<p>';
foreach($words as $key=>$w){
if(in_array($w, $answer_keys)){
if(in_array($key, $duplicate_word_keys[$w])){
echo "<span style='color:green;'>$w</span> ";
}
}else{
if(in_array($w, $answers) && !in_array($w, $marked)){
echo "<span style='color:red;'>$w</span> ";
}else{
echo "$w ";
}
}
}
echo '</p>';