Jump to content

Random phrase/line generator


kenneth009

Recommended Posts

Trying to develop a small app for my site. What is the code to generate a random # of lines (1 min - 5 max) and a random phrase for each line sourced from separate arrays (stored in txt files)?

do a multidimensional array, with numbers as the keys.

 

then use $a = rand(0, 10); or whatever you want for your range to get a random number.

 

echo $array[$a];

Thank you. Understand the multidimensional array part.

 

How do I prevent random duplication with the generated lines of text? Would it involve a separate random loop function? 

 

This would generate the random # of lines with <br> tags at the end.

 

The app needs to be able to generate 1 - 5 lines of text. Each line unduplicated.

<?php
$phrases = Array('Phrase A', 'Phrase B', 'Phrase C', 'Phrase D', 'Phrase E', 'Phrase F', 'Phrase G');
$out = Array();
for($i = 0;$i < 5; $i++)
{
$key = array_rand($phrases);
if(!in_array($phrases[$key], $out))
{
	$out[] = $phrases[$key];
	unset($phrases[$key]);
}
}

echo implode('<br />', $out);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.