Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-203731
Share on other sites

<?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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204087
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204203
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/42012-unscramble/#findComment-204206
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.