I need to create a script searching for words with ‘blanks’, which basically are % in sql.
$numberofblanks = 1; //max 13
$searchedword = "WORD";
$searchedwordsorted = "DORW";
Results given should be:
WORDY
WORLD
CROWD
SWORD
WORDS
DOWRY
ROWED
DROWN
DOWER
ROWDY
%word, w%ord, wo%rd, wor%d, word% would do, but what about more complicated queries, with 2 or more blanks?
Also was wondering if $searchedwordsorted is any helpful or it doesn’t really matter and it’s just waste of space in my table.
Thank you kindly for your help guys.
.mike
First I want to correct an error in your question. In your queries you mean
_not%. The%means any number of characters (zero or more). Use_to mean exactly one character.Now on to the solution… you don’t actually need the sorted word stored in the database. You could just do this:
If you have duplicate letters in your input, need to handle this correctly to ensure that all results contain all the duplicated letters. For example if the input is
FOO__you need to check that each word matches both%F%and%O%O%.Note that this approach will require a full scan of the table so it will not be particularly efficient. You could improve things slightly by storing the length of each word in a separate column and indexing that column.
If you have
sortedwordthen you can improve performance by omitting the%between duplicated letters since you know that they will appear consecutively insortedword. This could improve performance bceause it reduces the amount of backtracking required for failed matches.Another approach that requires
sortedwordto be present is as follows:Again this requires a full scan of the table. Again, if you have repeated letters you don’t need the
%between them.