I am trying to implement some code that checks the ispell dictionary to retrive a random word.
My host company tells me the dictionary is in 'usr/share/dict/words'
I put that into the function as the path, but it is not working. I also added an echo before the if(!$fp) return false to tell me that it had failed to get the word and that seems to be where it is bombint.
I am running this from a directory that is a ways down from my public_html directory.... so do I need to add something to point it further up the tree or what?
function get_random_word($min_length, $max_length)
// grab a random word from dictionary between the two lengths
// and return it
{
// generate a random word
$word = '';
// remember to change this path to suit your system
$dictionary = '/usr/share/dict/words '; // the ispell dictionary
$fp = @fopen($dictionary, 'r');
if(!$fp)
echo '<h1>Unable to open dictionary!</h1>';
return false;
$size = filesize($dictionary);
// go to a random location in dictionary
srand ((double) microtime() * 1000000);
$rand_location = rand(0, $size);
fseek($fp, $rand_location);
// get the next whole word of the right length in the file
while (strlen($word)< $min_length || strlen($word)>$max_length || strstr($word, "'"))
{
if (feof($fp))
fseek($fp, 0); // if at end, go to start
$word = fgets($fp, 80); // skip first word as it could be partial
$word = fgets($fp, 80); // the potential password
};
$word=trim($word); // trim the trailing \n from fgets
return $word;
}