我有一個在我的網站之一,搜索txt文件中的單詞並返回結果的anagram求解器。它會先返回2個字母的結果,然後當您向下滾動到12個字母的單詞時。我喜歡使用文本文件作爲我的數據庫,因爲它很快返回結果。顛倒anagram求解器以顯示最長的單詞第一個
我想顛倒結果的順序,以便首先顯示12個字母的單詞,然後向下滾動到2個字母的單詞。這裏是我的代碼,我將解釋我迄今爲止所做的更改會產生錯誤而不是我想要的結果。我很難理解它。
我會列出我的代碼以及一個鏈接,下載txt文件,如果你需要它是一個單詞列表。
鏈接文本文件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) ."\n";
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//
?>
我已經嘗試只是想扭轉「for」循環,這裏是當前工作的循環。請注意,有2個地方存在循環。
「for」循環,從2工作正常,但顯示結果目前 - 12
for($i = 0; $i < $wlen; $i++) {
,我試圖將其更改爲這個嘗試,並從12顛倒順序 - 2
for($i = $wlen; $i > 1; $i--) {
如果您發現$ wlen在此處聲明爲字符串長度,$ word是某人從表單字段輸入的單詞。
$wlen = strlen($word);
當我改變了「for」循環,我得到第29行是
foreach($b as $n => $c) {
林困惑這個錯誤,因爲$ c未至此之前的任何地方申報錯誤,但腳本如果您使用第一個「for」循環而不是第二個循環,則工作正常。
任何想法?
這將有助於如果您的變量名是更具描述性的。 $ v,$ o,$ a,$ b ...對於任何讀取您的代碼的人來說,這些都毫無意義。 (在一年的時間裏,你會忘記你爲什麼給他們起名,而且他們對你也沒有意義。) – Patashu 2013-05-09 06:34:45
另外,你可以像現在一樣生成相同的方式,將它們全部存儲在一個數組中,然後只是['array_reverse'](http://php.net/manual/en/function.array-reverse.php)($ results)'? – 2013-05-09 06:37:42