5.12 Checking for Duplicate Words


You want to find duplicate words in a paragraph or in a string.

Technique

Use 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'"; ?> 

Comments

This 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); ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net