Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My proposal is the following:</p> <p>Let's say the user input is: <code>ab*</code> where <code>*</code> is the wildcard. Count all the known letters and the wildcards and create an array where element 0 is the number of wildcards and every other element key is a known letter with a value the number of times it is given in the user input:</p> <pre><code>function GetArrayLetters($userInput) { ... // produces something like $letters = ( 0 =&gt; 1, 'a' =&gt; 1, 'b' =&gt; 1); return $letters; } </code></pre> <p>Using that array modify your query by adding the number of wildcards in each letter and at the word's length:</p> <pre><code># with only one wildcard, the query will become: SELECT * FROM scrabble WHERE a &lt;= 2 AND b &lt;= 2 AND c &lt;= 1 .... ... AND length &lt;= 3 </code></pre> <p>Now put the results somewhere (an array) and examine each word in turn. Go through each letter and subtract one for each known letter from your $letters array; If the value of a known letter is zero, subtract from element 0 instead (the wildcard). If you get a negative value for the wildcard, then discard the word:</p> <pre><code>foreach ($result_set AS $word) { $letters = GetArrayLetters($userInput); for ($i = 0; $i &lt; str_len($word); $i++) { $letter = substr($word, $i, 1); if ( array_key_exists( $letter, $letters )) { if ($letters[$letter]) &gt; 0 { $letters[$letter] -= 1; } else { $letters[0] -= 1; // else subtract from the wildcard } } else { $letters[0] -= 1; } if ($letters[0] &lt; 0) { // if wildcard falls bellow zero, discard the word } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload