Jump to content

Selecting number from a TXT file


soltek

Recommended Posts

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.

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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.

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.