Can somebody post a little php snippet for the following problem? I have a textfile like this: I {love|like|need} {coffee|tea}. He is {cool|nice|awesome} {.|..|!} Are you {hungry|tired|angry}? The little php snippet should select one line of the textfile at random and create a random sentence from it, every time i start the php-script. Example: After starting the script, it selects line 1: "I {love|like|need} {coffee|tea}." Then it creates a random sentence like "I like tea." or "I love coffee." (or something like this) from it.
<?php function rand_wrd($input){ if (!is_array($input)){ return preg_replace_callback('~\{(.+?)\}~', 'rand_wrd', $input); } else { $input = explode("|", $input[1]); return $input[array_rand($input)]; } } //the text file containing the sentences.... $file = file('textfilename.txt'); $file = array_map('rand_wrd', $file); //display a random sentence... echo $file[array_rand($file)]; ?> PHP: