How would i randomly select a word from the current webpage using php

Discussion in 'PHP' started by ade92uk, Feb 5, 2009.

  1. #1
    Hi, i would like to, using php, randomly select a word from the current page, which i have already managed to get into a string.

    I would like to use php to randomly choose any of the words, the full word.
     
    ade92uk, Feb 5, 2009 IP
  2. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you have the words in an array you can do:

    
    $words = array("word1","word2","word3");
    $random = rand(0, count($words)-1);
    echo $words[$random];
    
    
    
    PHP:
    To get the words into an array from a string use the explode() function on space characters or something..
     
    lui2603, Feb 5, 2009 IP
  3. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    And you may also need to use array_diff() function to filter out filler words like 'and', 'or', 'be', 'may be', etc. if you need.
     
    rohan_shenoy, Feb 5, 2009 IP
  4. ade92uk

    ade92uk Banned

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks, but it dosn't seem to be working for me:

    I have made this, using google define:
    http://obolynx.com/developments/import.php?word=whom

    and i would like to select each word on the page in turn and have it hyperlinked, this is what i have so far codewise:

    
    for($i = 0, $size = sizeof($matches[0]); $i < $size; ++$i) {
         
    	  // assuming the string is called "someString"
         $words = explode(" ", $matches[0][$i]);
         $index = rand(0, count($words)-1);
         $choice = $words[$index];
    	 $href = '<a href="import.php?word=';
    	 $href .= $choice;
    	 $href .= '">';
    	 $href .= $choice;
    	 $href .= '</a>';
    	 echo $href;
    	 echo $choice;
    	 $randomWord = array_rand($words);
    	 echo $randomWord;
    
    	}
    
    Code (markup):
    Thanks
     
    ade92uk, Feb 5, 2009 IP