PHP/MySQL Anagram Finder

If you find this code useful and would like to encourage me to post more goodies here, you could buy me something from my wish lists at Amazon.com or at MusicDirect.com

The following is a little something I came up with to find anagrams by using PHP and a MySQL database of words and their letter counts.

Find Anagrams

Table structure:

CREATE TABLE `word_list` (
`word` varchar(32) binary NOT NULL,
`letters` varchar(255) binary NOT NULL,
PRIMARY KEY  (`word`),
KEY `letters` (`letters`)
) TYPE=MyISAM;

Sample data:

word        letters
---------  ---------------------------
aals       a:2,l:1,s:1
aardvark   a:3,d:1,k:1,r:2,v:1
aardvarks  a:3,d:1,k:1,r:2,s:1,v:1
aardwolf   a:2,d:1,f:1,l:1,o:1,r:1,w:1

The code:


<?php
if(isset($_GET['word']))
{
   
$word strtolower(trim($_GET['word']));
   if(
ctype_alpha($word))
   {
      
$connx mysql_connect('******','******','******') or die("Connx");
      
mysql_select_db('******') or die(mysql_error());
      
$chars characterCount($word);
      
$query "SELECT word FROM word_list WHERE letters = '$chars' ORDER BY word";
      
$result mysql_query($query) or die(mysql_error());
   }
   else
   {
      
$error "Invalid word '$word': must consist only of letters a-z.";
   }
}

function 
characterCount($word)
{
   
$counts = array();
   foreach(
count_chars(strtolower($word), 1) as $char => $count)
   {
     
$counts[] = (chr($char).':'.$count);
   }
   return(
implode(','$counts));
}
require_once(*****);
head("Anagram Finder","PHP/MySQL Anagram Finder: finds single-word anagrams");
require_once(*****);
?>
<h2>PHP/MySQL Anagram Finder</h2>
<?php
wishList
();
?>
<p>The following is a little something I came up with to find anagrams by using PHP
and a MySQL database of words and their letter counts.</p>

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='get' style='clear:both'>
<fieldset>
<legend>Find Anagrams</legend>
<label>Enter scrambled word:
<input type='text' name='word' size='10' maxlength='12'
<?php
if(!empty($word))
{
   echo 
" value='$word'";
}
?>>
</label>
<input type='submit' value='Submit'>
</fieldset>
</form>
<?php
if(!empty($error))
{
   echo 
"<p style='color:red'>$error</p>\n";
}
if(isset(
$result))
{
   if(
mysql_num_rows($result))
   {
      echo 
"<h4>Matches for '$word':</h4>\n<ul>\n";
      while(
$row mysql_fetch_row($result))
      {
         echo 
'<li>'.$row[0]."</li>\n";
      }
      echo 
"</ul>\n";
   }
   else
   {
      echo 
"<p>Sorry, no anagrams found for '$word'</p>\n";
   }
}
?>
<p>Table structure:</p>
<pre>CREATE TABLE `word_list` (
`word` varchar(32) binary NOT NULL,
`letters` varchar(255) binary NOT NULL,
PRIMARY KEY  (`word`),
KEY `letters` (`letters`)
) TYPE=MyISAM;</pre>
<p>Sample data:</p>
<pre>word        letters
---------  ---------------------------
aals       a:2,l:1,s:1
aardvark   a:3,d:1,k:1,r:2,v:1
aardvarks  a:3,d:1,k:1,r:2,s:1,v:1
aardwolf   a:2,d:1,f:1,l:1,o:1,r:1,w:1
</pre>
<p>The code:</p>
<pre><?php include 'code.php'?></pre>
<?php
foot
('PHP_and_MySQL');
?>