Results 1 to 2 of 2

Thread: Display Random Text?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Display Random Text?

    Hi guys, Imagine i have this


    $text1 = "hi";
    $text2 = "hello";
    $text3 = "hey";
    $text4 = "Good Day";


    How is it possible to display only one of those strings above but also display the ones that have not been chosen for example output would end up like


    "Good Day"


    we also have Hey,Hello,Hi



    so it shows one random output but also displays the remaining that was not chosen randomly elsewhere on the page?

    Hope that makes sense

    Cheers

  2. #2
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Display Random Text?

    Code:
    $texts = array('hi','hello','hey','Good Day');
    $text = array_rand($texts);
    echo '"' . $texts[$text] . '"<br />we also have ';
    unset($texts[$text]);
    $ln = count($texts);
    for($i=0;$i<$ln;$i++){
    if(isset($texts[$i])){
    echo ucfirst($texts[$i]) . ($i<$ln-1 ? ',' : '');
    }
    }
    This will do exactly what you typed. It will echo a random element of the array then echo all the other values left over while capitalizing the first letter of each of those values and seperating them by a comma. Right after writing this I realized you could also do:

    Code:
    implode(',',$texts);
    Instead of the loop.

    Also if you search "php random array" on Google the first link is to the array_rand() php function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width