serial Posted March 9, 2007 Share Posted March 9, 2007 hi all is there any function that could unscramble word i have 2 files 1 with scambled letters of a word but they change every 2 seconds and then i have a file with the correct words and i just wondered if there was a function to do this Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/ Share on other sites More sharing options...
Orio Posted March 9, 2007 Share Posted March 9, 2007 I once wanted to make an anagram solver (that unscrambles letters into words) but I never actually made it because I didn't have a word list. This my idea (it worked on a small word list with 1000 words, there shouldn't be a problem to do it with a bigger list): Take a word list. Make a table with the columns- word, sort. For every word, enter the word into the "word" column, and in the sort column enter the word when it's letters are sorted alphabetically (see the function attached). Now, every time you want to unscramble a bunch of letters, you pass then through the function attached, and search in the database for the words that their sort is the sort you made ("SELECT * FROM `table` WHERE sort='$sort'"). And that's it The function: <?php //This requires php5 function word_sort($word) { $letters = str_split($word); sort($letters); return implode("",$letters); } ?> I hope I've helped Orio. Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-203731 Share on other sites More sharing options...
serial Posted March 9, 2007 Author Share Posted March 9, 2007 i didn't really want to make a database really as it is only 50 words long i have the 50 words in a .txt file and then i open the scambled file which chooses 20 scambled word out of the 50 (which are the 50 states). i just wondered if i could do it like that Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-203745 Share on other sites More sharing options...
serial Posted March 10, 2007 Author Share Posted March 10, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204059 Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 <?php $file = file('50states.txt'); // make sure your words are on seperate lines this returns an array $scrambled = file('scrambled.txt'); // same deal as above ...... ?> After that I am not sure what you are asking, do you want to match the scrambled words to the one of the 50 state words? --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204087 Share on other sites More sharing options...
serial Posted March 10, 2007 Author Share Posted March 10, 2007 ok what i want is to open a file (scrambled.txt) which will have 20 scrambled states eg. wnekroy oiho and i want open another file(unscramled.txt which have all the word that are not scramled) to unscramble them and print them out like this: newyork:ohio: thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204203 Share on other sites More sharing options...
Orio Posted March 10, 2007 Share Posted March 10, 2007 As I suggested... <?php //This requires php5 function word_sort($word) { $letters = str_split($word); sort($letters); return implode("",$letters); } $scrambled = file("scrambled.txt"); $unscrambled = file("states.txt"); foreach ($scrambled as $w) { foreach($unscrambled as $w2) { if(word_sort(trim($w)) == word_sort(trim($w2))) echo $w." is ".$w2."<br>"; } } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204206 Share on other sites More sharing options...
serial Posted March 10, 2007 Author Share Posted March 10, 2007 thank you very much orio and thanks all thats great btw orio your site looks pritty good Quote Link to comment https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204208 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.