soltek Posted May 31, 2011 Share Posted May 31, 2011 Hey there, this I've no idea how can be done with PHP. I'm looking for a way to randomly select a 7 digits number from a *.txt and then print it on a webpage. Do any one know how can this be done? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/ Share on other sites More sharing options...
QuickOldCar Posted May 31, 2011 Share Posted May 31, 2011 here's one way make a text file named numbers.txt same location as this script, or rename it to your text file <?php $my_file = "numbers.txt"; if (file_exists($my_file)) { $data = file($my_file); $number = $data[rand(0, count($data) - 1)]; echo $number; } else { echo "$my_file does not exist"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223248 Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 Can use preg_match() to single out the number in the file them echo it Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223249 Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 How are you storing these numbers? Separated by commas, linebreaks, etc? Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223255 Share on other sites More sharing options...
soltek Posted June 1, 2011 Author Share Posted June 1, 2011 How are you storing these numbers? Separated by commas, linebreaks, etc? by a space, xyph. But I can replace the space for a 'space&comma' Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223265 Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 Space is fine untested <?php $data = file_get_contents('yourfile.txt'); // grab the whole file into a variable $array = explode(' ',$data); // explode the data into an array, by spaces $max = count($array); // find out how many numbers were returned echo $array[rand(0,$max)]; // echo out a random value. ?> Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223268 Share on other sites More sharing options...
soltek Posted June 1, 2011 Author Share Posted June 1, 2011 It works almost fine. The problem is sometimes it doesnt select anything. Even in the source code of the rendered page it doesnt print any number. my php code is: <?php $data = file_get_contents('yourfile.txt'); // grab the whole file into a variable $array = explode(',',$data); // explode the data into an array, by spaces $max = count($array); // find out how many numbers were returned echo 'Thizzladen, your lucky number is ', $array[rand(0,$max)]; // echo out a random value. ?> My txt file has this: ,00125,14589, ´ Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223663 Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 the leading zeroes could cause an issue... try trimming your values $var = ltrim($var, '0'); Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223671 Share on other sites More sharing options...
soltek Posted June 1, 2011 Author Share Posted June 1, 2011 fugix, I tried that but I think I did something wrong when editing my code. How would the code look after your tweak? Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223686 Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 well you would need to use a foreach() loop to extract the values of the arry and run ltrim() on them...but this would throw off the original $array[rand(0,$max)] logic Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223697 Share on other sites More sharing options...
Pikachu2000 Posted June 1, 2011 Share Posted June 1, 2011 Is it possible that there are some spots in the text file where there are 2 consecutive commas? That's the most likely cause of the occasional empty result, as far as I can see. Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223703 Share on other sites More sharing options...
xyph Posted June 1, 2011 Share Posted June 1, 2011 You don't want a trailing comma. print_r( $array ); to see what's happening. Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223705 Share on other sites More sharing options...
DavidAM Posted June 1, 2011 Share Posted June 1, 2011 My txt file has this: ,00125,14589, ´ Any idea? You don't want a trailing comma. print_r( $array ); to see what's happening. You don't want a LEADING comma either Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223714 Share on other sites More sharing options...
PFMaBiSmAd Posted June 1, 2011 Share Posted June 1, 2011 Why not just generate a random 7 digit number when you need one? Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223715 Share on other sites More sharing options...
Pikachu2000 Posted June 1, 2011 Share Posted June 1, 2011 My txt file has this: ,00125,14589, ´ Any idea? Wait a second. Is that the only thing in the file? Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223724 Share on other sites More sharing options...
soltek Posted June 1, 2011 Author Share Posted June 1, 2011 The commas on the text are there 'cause I thought the problem might be with the space, so I changed to a comma. Anyway, I also cant generate random numbers, I just need specific ones, those that are in the txt file. I added: print_r($array) after: echo $array[rand(0,$max)] And it prints: Array ( [0] => 00125 [1] => 14589 ) or 00125 Array ( [0] => 00125 [1] => 14589 ) or 14589 Array ( [0] => 00125 [1] => 14589 ) The whole code is: <?php $data = file_get_contents('yourfile.txt'); // grab the whole file into a variable $array = explode(' ',$data); // explode the data into an array, by spaces $max = count($array); // find out how many numbers were returned echo $array[rand(0,$max)]; // echo out a random value. print_r($array); ?> And the txt only has those two numbers and one space separating them. Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223780 Share on other sites More sharing options...
Pikachu2000 Posted June 1, 2011 Share Posted June 1, 2011 Assuming you're going to use spaces as the separator, that should work just fine, with one change. Since the array indices start at zero, you need to subtract 1 from the count when calculating $max. Without doing that, the possible values would be 0, 1 and 2, but only index numbers 0 and 1 exist, explaining why you were getting empty values. If you had error reporting on, you should have gotten an undefined index warning. $max = count($array) - 1; // find out how many numbers were returned Quote Link to comment https://forums.phpfreaks.com/topic/238044-selecting-number-from-a-txt-file/#findComment-1223789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.