Actually, it still doesn't seem to work when looking through a dictionary.
If I enter these characters:
Code:
b,a,t,t,e,r,y,g,j,u,b,t
No word is actually found, the word that should be found is "battery". It is in the database:
Code:
ID word
6909 battery
If I just stick with the code below, it finds "battery" but also another 42 words as well. Which should not happen. If I add that extra bit of code:
PHP Code:
$chars = array_diff($chars, $word_split);
Then no words are found at all.
PHP Code:
//Add all characters to Array while ($g != 13) { $c_char = mysql_real_escape_string($_GET['char'.$g]); //Current character if ($c_char) { $chars[] = $c_char; } $g++; }
//Start fecthing database data $query = "SELECT * FROM dictionary WHERE LENGTH(word) = ".$length." ORDER BY ranking DESC"; $result = mysql_query($query);
if(!$result) { $return_data = "Error connecting to dictionary!"; }
//Fetch word matching the selected length while($row = mysql_fetch_array($result)) { $data = trim($row['word']); $word_split = str_split($data);
$aString = str_split($string); $diff = array_diff($aString, $aChars); $aChars = array_diff($aChars, $aString); if (empty($diff)) { printf("Characters found in %s" . PHP_EOL, $string); } print_r($aChars);
Will result in:
Code:
Characters found in battery
Array ( [7] => g [8] => j [9] => u )
As I said it will consume all the characters within the strings when calculating the difference. The only remaining characters are g, j and u, and that doesn't really form any possible words. Therefore a call to the empty($diff) will result in a lot of falses to follow unless your word is gju, guj, jgu, jug, ugj, or ujg.
So what exactly is it you are trying to do here? It almost looks like a spell checker. If this is the case you may be better off using a levenshtein and metaphone to find similar words. Both of these can be used when inserting data into the database to create two additional columns to search on, then calculate the word input and select from the database where they match. If the levenshtein and metaphone both match in a single row, that should be the literal input, while you can sort the results remaining by either spells with the same characters (levenshtein) or sounds the same (metaphone).
Basically, what it should do is search for words which can be made with the given characters, and are a defined length. So far it work's but it's finding too many words as it is using some characters more than once.
Once a character has been used it shouldn't be used again.
After reading through that code and researching many functions that I have not come across I think that will solve my problem.
I will have another read through tomorrow and try and implant it into my code to see if it does what I want. I don't like using someone elses code until I understand exactly what it does.
I don't see the point in coding something if it's all copy and paste, don't learn anything like that. So before I will use this I need to fully understand it.
Quick question, this line here function canSpellsWord($sWord, array $aFromChars) , do you need to put "array" in front of the, well array so that it stays an array whilst being passed to the function?
No, that only forces it to only accept an array. I wish PHP at least type hint strong for all primitive datatypes, but only array and a class type can be used for the datatype in the signature.
This should work, but instead blank values are displayed... Roughly about 4000
PHP Code:
//Start fecthing database data $query = "SELECT * FROM dictionary WHERE LENGTH(word) = ".$length." ORDER BY ranking DESC"; $result = mysql_query($query);
if(!$result) { $return_data = "Error connecting to dictionary!"; }
//Fetch word matching the selected length while($row = mysql_fetch_array($result)) { $data = trim($row['word']);