Skip to content

Instantly share code, notes, and snippets.

@jwalanta
Created March 18, 2013 07:37
Show Gist options
  • Save jwalanta/5185613 to your computer and use it in GitHub Desktop.
Save jwalanta/5185613 to your computer and use it in GitHub Desktop.
4Pics1Word word finder
<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