This may seem a bit evil, but you will probably get better performance by loading the dictionary into an array in the form of a dictionary tree, but in the reverse order of words, for example:
array( 'r' => array( 'u' => array(), // -- words ending in 'ur' would end up in here 'a' => array(), // -- words ending in 'ar' would end up here 'e' => array( // -- words ending in 'er' would end up in here 'm' => array( 'm' => array( // -- jackhammer will be kept further up here
Then, looking up.
$reverseWord = ""; // -- Incoming 'word' string goes here, in reverse. $dictionary = [structure above]; $dictionaryPosition = $dictionary; $dictionaryHistory = ""; for( $i = 0, $l = strlen($reverseWord); $i < $l; $i++ ) { $char = $reverseWord[$i]; // -- If this character doesn't exist in this dictionary position, we've reached the end if( !isset($dictionaryPosition[$char]) ) break; // -- log this character $dictionaryHistory = $char . $dictionaryHistory; // -- Climb up the tree $dictionaryPosition = $dictionaryPosition[$char]; } // -- $dictionaryHistory now contains the word you're looking for.
Each array should contain no more than 26 entries (taking into account only alphabetic characters), so you see, at best, 26 * n queries of one character each. Even with a word depth of 20 characters, it is infinitely better than repeating several words with a length of 50 thousand words.
source share