TechniqueUse the array methods to check for duplicate words in a single string: <?php $seen = array(); $paragraph = "The ugly lady chased the handsome man"; $paragraph = preg_split ("/\s+/", $paragraph); foreach ($paragraph as $word) { $seen[strtolower ($word)]++; } print "There were $seen[the] occurrences of the word 'the'"; ?> CommentsThis script would increment up to the number of case-insensitive occurrences of each word. To access the number of occurrences of a given word, just place the word as the string index of the array. If you want to get the string position of each of the duplicates, you can use the following function: <?php function get_dupes ($str) { $str = strtolower ($str); $words = preg_split ("/\s+/", $str); $seen = array(); $start_pos = array(); $i = 0; foreach ($words as $word) { // loop through an array of words // 2-d associative array containing word positions $seen[$word][$i] = strpos ($str, $word, $start_pos[$word]); // assign the starting position to avoid repeats $start_pos[$word] = $seen[$word][$i] + strlen ($word); $i++; } return ($seen); } $str = "The The the Hello Truck Hello The the Jester Rye"; $duplicates = get_dupes ($str); var_dump ($duplicates); ?> |