Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.

Let us know at info@questionaries.org

Random Sentences - PHP Snippet Needed?

8 like 0 dislike
Random Sentences - PHP Snippet Needed?
asked 2 years ago by monirulislam (51,620 points)

1 Answer

0 like 0 dislike
 
Best answer
<?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)];

?>
answered 2 years ago by monirulislam (51,620 points)

Related questions

11 like 0 dislike
1 answer
asked 2 years ago by webmaster (25,380 points)
10 like 0 dislike
2 answers
8 like 0 dislike
1 answer
asked 2 years ago by monirulislam (51,620 points)
0 like 0 dislike
1 answer