I have an anagram solver on one of my sites that searches for words in a txt file and returns the results. It will return the results from 2 letter words first then as you scroll down up to 12 letter words. I like using a text file as my db because it returns the results so quickly.
I want to reverse the order of results so that it shows 12 letter words first then as you scroll down to 2 letter words. Here is my code and I will explain what changes I have made so far that produce errors and not the results I want. I am having a hard time understand it.
I will list my code as well as a link to download the txt file which is a word list if you need it.
Link to text file http://guildfit.com/combined.txt
<?
//Code Here//
$l = 'apple';
if(!empty($l)) { ?>
<table cellpadding="5" cellspacing="0" border="0" width="800" style="text-align:left">
<tr>
<td style="text-align:center; font-weight:bold; width:100%" colspan="20">Click on words to get definitions</td>
</tr>
<?
$time_start = microtime(true);
$l = $l;
$l = trim($l);
$l = strtolower($l);
$l = str_replace(' ', '_', $l);
$l = preg_replace('/[^\w]/', '', $l);
$len = strlen($l);
$a = array('a' => 1, 'b' => 1, 'c' => 1, 'd' => 1, 'e' => 1, 'f' => 1, 'g' => 1, 'h' => 1, 'i' => 1, 'j' => 1, 'k' => 1, 'l' => 1, 'm' => 1, 'n' => 1, 'o' => 1, 'p' => 1, 'q' => 1, 'r' => 1, 's' => 1, 't' => 1, 'u' => 1, 'v' => 1, 'w' => 1, 'x' => 1, 'y' => 1, 'z' => 1);
function contains($word) {
$wlen = strlen($word);
if($wlen < 2 || $wlen > 12) return FALSE;
//for($i = $wlen; $i > 1; $i--) {
for($i = 0; $i < $wlen; $i++) {
$w[$i] = $word[$i];
}
$b = $GLOBALS['b'];
foreach($b as $n => $c) {
foreach($w as $k => $v) {
if($v == $c) {
unset($w[$k]);
unset($b[$n]);
break;
}
}
}
//echo "count: ". count($w) ."
";
if(count($w) > 0) return FALSE;
return TRUE;
}
//for($i = $wlen; $i > 1; $i--) {
for($i = 0; $i < $len; $i++) {
$b[$i] = $l[$i];
unset($a[$l[$i]]);
}
$e = 'cat combined.txt';
while(list($k) = each($a)) {
if(!$e) {
$e = "grep -v $k combined.txt";
} else {
$e .= ' | grep -v '. $k;
}
}
exec($e, $o);
settype($w, 'array');
foreach($o as $v) {
if(contains($v)) {
$w[] = $v;
}
}
function mycmp($a, $b) {
$ca = strlen($a);
$cb = strlen($b);
if($ca == $cb) return 0;
if($ca > $cb) return 1;
return -1;
}
usort($w, 'mycmp');
$wc = 0;
foreach($w as $v) {
$c = strlen($v);
if($wc != $c) {
echo '';
echo "<tr><td style='color:#FF0000'><h2>Words anagram from <em>$l</em></h2></td><td style='color:#FF0000'><h2 style='text-align:center'>Scrabble Points</h2></td><td style='color:#FF0000'><h2 style='text-align:center'>Words With Friends Points</h2></td></tr>";
echo "<tr><td colspan=2><h2>$c letter words</h2></td></tr>";
}
$wc = $c;
$getvalues = str_split($v);
$thisletter1 == 0;
$thisletter1wwf == 0;
// Scrabble Points
foreach($getvalues as $letter) {
if ($letter == 'a' || $letter == 'e' || $letter == 'i' || $letter == 'l' || $letter == 'n' || $letter == 'o' || $letter == 'r' || $letter == 's' || $letter == 't' || $letter == 'u') {
$thisletter = 1;
} elseif ($letter == 'd' || $letter == 'g') {
$thisletter = 2;
} elseif ($letter == 'b' || $letter == 'c' || $letter == 'm' || $letter == 'p') {
$thisletter = 3;
} elseif ($letter == 'f' || $letter == 'h' || $letter == 'v' || $letter == 'w' || $letter == 'y') {
$thisletter = 4;
} elseif ($letter == 'k') {
$thisletter = 5;
} elseif ($letter == 'j' || $letter == 'x') {
$thisletter = 8;
} elseif ($letter == 'q' || $letter == 'z') {
$thisletter = 10;
}
$thisletter1 = $thisletter1+$thisletter;
}
// WWF Points
foreach($getvalues as $letterwwf) {
if ($letterwwf == 'a' || $letterwwf == 'e' || $letterwwf == 'i' || $letterwwf == 'o' || $letterwwf == 'r' || $letterwwf == 's' || $letterwwf == 't') {
$thisletterwwf = 1;
} elseif ($letterwwf == 'd' || $letterwwf == 'l' || $letterwwf == 'n' || $letterwwf == 'u') {
$thisletterwwf = 2;
} elseif ($letterwwf == 'g' || $letterwwf == 'h' || $letterwwf == 'y') {
$thisletterwwf = 3;
} elseif ($letterwwf == 'b' || $letterwwf == 'c' || $letterwwf == 'f' || $letterwwf == 'm' || $letterwwf == 'p' || $letterwwf == 'w') {
$thisletterwwf = 4;
} elseif ($letterwwf == 'k' || $letterwwf == 'v') {
$thisletterwwf = 5;
} elseif ($letterwwf == 'x') {
$thisletterwwf = 8;
} elseif ($letterwwf == 'j' || $letterwwf == 'q' || $letterwwf == 'z') {
$thisletterwwf = 10;
}
$thisletter1wwf = $thisletter1wwf+$thisletterwwf;
}
echo "";
echo '';
$thisletter1 = 0;
$thisletter1wwf = 0;
}
$time_end = microtime(true);
$time = round($time_end - $time_start, 5);
echo "<tr><td style='text-align:center' colspan='20'>Found <strong>". count($w) ."</strong> words in <strong>$time</strong> seconds</td></tr></table>";
}
// End Code Here//
?>
What I have tried is simply trying to reverse a "for" loop, here is the loop that works currently. Note there are 2 locations where to loop exists.
The current "for" loop that works fine but shows results from 2 - 12
for($i = 0; $i < $wlen; $i++) {
and I tried to change it to this to try and reverse the order from 12 - 2
for($i = $wlen; $i > 1; $i--) {
If you notice $wlen is declared as a string length here, $word being the word someone inputs from a form field.
$wlen = strlen($word);
When I change the "for" loop I get an error at line 29 which is
foreach($b as $n => $c) {
Im confused about this error because $c is not declared anywhere before this point, yet the script works fine if you use the first "for" loop and not the second.
Any ideas?
Your ordering should be reversed if you change the comparison function used to sort $w
, the array of anagrams.
function mycmp($a, $b) {
$ca = strlen($a);
$cb = strlen($b);
if($ca == $cb) return 0;
if($ca > $cb) return 1;
return -1;
}
Change it to
function mycmp($a, $b) {
$ca = strlen($a);
$cb = strlen($b);
if($ca == $cb) return 0;
if($ca > $cb) return -1;
return 1;
}
And see what happens.
Following Patashu's answer, the compare function could be much simpler (and more easier to see what is happening when it is reversed):
// Smallest to largest:
function stringLengthCompare($a, $b) {
return strlen($a) - strlen($b)
}
// Largest to smallest:
function stringLengthCompare($a, $b) {
return strlen($b) - strlen($a)
}