I want to create a jumble game that randomly selects a word from a text file and jumbles it.

This is the code:

<?php
error_reporting(E_ALL);
$filename = "words4jumble.txt";
$fp = fopen($filename, "r");
$filecontents = fread($fp, filesize($filename));
fclose($fp);
$exploded = explode(":", $filecontents);
$size = sizeof($exploded)-1;
$num = rand(0, $size);
$word = str_shuffle($exploded[$num]);
print "Your word is $word. Have fun! <form name=\"jumble\" method=\"post\" 
action=\"jumblecheck.php\"><input name=\"realword\" type=\"hidden\" value=\"$exploded[$num]\">";
?>
<input type="text" name="wordunjumble"><p><input type="reset">&nbsp;&nbsp;&nbsp;
<input type="submit" value="I solved it!">
</form>

I get a fatal error saying that str_shuffle() was called to an undefined function. Please help me! I have attached a text file with the words if it's any help at all.

    make sure the server your running it on is PHP 4 >= 4.3.0

      Thanks for the tip...sorry that was a dumb question...is there any way to randomly shuffle an array without using str_shuffle()?.

        I haven't tested this but I think it might work

        $aryWord = explode("",$word);

        now $aryWord is an array of the letters that make up $word

        shuffle($aryWord);

        now $aryWord is shuffled

        $newWord = implode("",$aryWord);

        now $newWord is your shuffled word

        I think this'll be a fun little exercise so I'm off to do a little test script now.

          and here is the example which will run on older and newer versions of php.

          I used the original poster's word file but I made it use line returns instead of : just make sure you ftp it in ascii mode as not changing the line return character could break the program also no trailing spaces on the words.

            Thanks for the tip. This thread is now resolved.

              Write a Reply...