SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prompt for a random word in a document

    I'm looking for a way to prompt a visitor for a random word or phrase in a document. For example, the visitor would be prompted to enter the first word on the third paragraph. The word required would be randomly selected from the document.

    I've seen something like this before but can't seem to track it down. If someone could point me in the right direction it would be a big help.

  2. #2
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm,
    generally basic idea seems rather straight forward.

    PHP Code:
    $paragraph "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    Why do we use it?"
    ;

    //Create Random #
    srand(mktime());
    $rand rand();

    //Number of words in paragraph
    $words explode(" "$paragraph);
    $num_words count($words); 

    //Make sure random value is in paragraph
    $word_index $rand $num_words;

    session_start();
    $_SESSION['word'] = $words[$word_index];

    print 
    "Please enter " ($word_index+1) . " word from: '" $paragraph ."'"
    That's a pretty straight forward way of doing it. You can make it more complicated by making the paragraph chosen random. Using sentences instead of words so
    PHP Code:
    explode('. '$paragraph); 
    Probably right some logic so you can say the last word of the 3rd sentence.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's a big help. I appreciate it very much.


Bookmarks

Posting Permissions

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