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];

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.