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.

// assuming the string is called "someString"
  $words  = explode(" ", $someString);
  $index  = rand(0, count($words)-1);
  $choice = $words[$index];
// assuming the string is called "someString"
  $words  = explode(" ", $someString);
  $index  = rand(0, count($words)-1);
  $choice = $words[$index];

OK

Once you have an array $words you can just use

$randomWord = array_rand($words);

I've been using PHP for years and I still have moments where I find out after I finish something that there is a built-in function to do it. Mainly on small things like this that shouldn't be built-in. :)

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;

	}

Thanks

It doesn't show the word or the link, go to the page, no links. $href is not showing.

I was using it to test it when it wasn't working, none of them show anything

for($i = 0, $size = sizeof($matches[0]); $i < $size; ++$i)

What are you trying to do here? You have 4 statements in a for loop, which only every takes 3.

$size = sizeof($matches[0]);
for($i = 0, $i < $size; ++$i)
This article has been dead for over six months. Start a new discussion instead.