I need to get two random numbers from 1 to 57 say, how do I not get the 0.
I have a paragraph of text and am trying to select two numbers but every now and then I get a 0 which as the numbers start from 1 there is no word for 0. the code I have is as follows it's a bit messy. 🙂
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
$text = "A jukebox with one song is unlikely to be popular (except perhaps in some very, very scary bars), so pretty soon we'll have to start thinking about producing a catalog of available songs and a playlist of songs waiting to be played. Both of these are containers: objects that hold references to one or more other objects.";
$word_count = count_words($text);
//echo " - $word_count - ";
{
srand(time());
// the random number 1 to 57
for ($i=1; $i < $word_count; $i++)
{
// enter the max number as %xx
$random = (rand()%$word_count);
$slot[] = $random;
}
print(" Enter the $slot[0]");
print(" and the $slot[1]");
print (" word in the paragraph located at www.XXXXXXXXX.com/validate.html");
}
?>
<p>Word 1 = </p>
<p>Word 2 =</p>
<p>Confirm</p>
<p> </p>
<?
// print("<td width=\"33%\"><center>$slot[2]</td>");
// if($slot[0] == $slot[1] && $slot[0] == $slot[2])
{
print("If confirm ok the show this - $word_count - ");
// exit;
}
?>
</body>
</html>
<?php
function count_words($string) {
$word_count = 0;
$string = eregi_replace(" +", " ", $string);
$string = explode(" ", $string);
while (list(, $word) = each ($string)) {
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $word)) {
$word_count++;
}
}
return($word_count);
}
?>