Find Duplicate Words in Text Script

Find Duplicate Words in Text Script is used to identify words that appear more than once in a string.

Demo :



Type Text  

Example :

Type Text :baa baa black sheep

if you need to find duplicate words in text script you will copy and use to your website.


<?php
  $word='baa baa black sheep';
  $str = trim($word);
  $str = ereg_replace('[[:space:]]+', ' ', $str);
  $words = explode(' ', $str);
  foreach ($words as $w)
  {
  $wordStats[strtolower($w)]++;
  }
  foreach ($wordStats as $k=>$v)
  {
  if ($v >= 2) { print "$k \r\n"; }
  }
?>

The first task here is to identify the individual words in the sentence or paragraph. You accomplish this by compressing multiple spaces in the string, and then decomposing the sentence into words with explode(), using a single space as [the] delimiter. Next, a new associative array, $wordStats, is initialized and a key is created within it for every word in the original string. If a word occurs more than once, the value corresponding to that word's key in the $wordStats array is incremented by 1.

Once all the words in the string have been processed, the $wordStats array will contain a list of unique words from the original string, together with a number indicating each word's frequency. It is now a simple matter to isolate those keys with values greater than 1, and print the corresponding words as a list of duplicates.

Free Download Script :

If you need find duplicate words in text script click and download from following link.

Download




Content