$str = 'Love a and peace love a and peace love a and peace love a and peace love a and peace love a and peace' ; // Build a histogram mapping words to occurrence counts $hist = array (); // Split on any number of consecutive whitespace characters foreach ( preg_split ( '/\s+/' , $str ) as $word ) { // Force all words lowercase to ignore capitalization differences $word = strtolower ( $word ); // Count occurrences of the word if ( isset ( $hist [ $word ])) { $hist [ $word ]++; } else { $hist [ $word ] = 1 ; } } // Once you're done, extract only the counts $vals = array_values ( $hist ); rsort ( $vals ); // Sort max to min // Now that you have the counts, analyze and decide valid/invalid var_dump ( $vals );