Created
March 18, 2013 07:37
-
-
Save jwalanta/5185613 to your computer and use it in GitHub Desktop.
4Pics1Word word finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head><title>4pics1word</title></head> | |
<body> | |
<h3>4pics1word</h3> | |
<form method="GET" action=""> | |
Letters <input type="text" name="w" maxlength="12" /> e.g., "lrckioodubue"<br /> | |
Length <input type="text" name="l" maxlength="1" /> e.g., "7"<br /> | |
<input type="submit" value="Find Words" /> | |
</form> | |
<?php | |
// get letters and length | |
$w = strtolower($_GET['w']); | |
$l = $_GET['l']; | |
// find words in dictionary | |
// this will also return words that use same letter more than once | |
$cmd = "egrep '^[$w]{".$l."}\$' /usr/share/dict/words"; | |
// check if words are valid combinations | |
$lines = explode("\n",shell_exec($cmd)); | |
foreach ($lines as $word){ | |
if (check($word, $w)) echo "$word\n"; | |
} | |
// function to check if the words can be formed | |
// using combination of given letters | |
function check($word, $letters){ | |
for ($i=0;$i<strlen($word);$i++){ | |
if (preg_replace("/".substr($word,$i,1)."/","",$letters, 1) == $letters) return false; | |
else $letters = preg_replace("/".substr($word,$i,1)."/","",$letters, 1); | |
} | |
return true; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment