1

I need to save many variables in text file and later retrieve any one random data from the text document.

For Example ...i need to add 31231231 to the below text file

213123123
213123124
123412321

and i need to retrieve random value from above text file ..for example 213123124 from above text file

5
  • php.net/manual/en/book.filesystem.php Commented Jan 29, 2012 at 9:27
  • 1
    $handle = fopen("lol.txt", "a"); foreach($_GET as $variable => $value) { fwrite($handle, $variable); } fwrite($handle, "\r\n"); Commented Jan 29, 2012 at 9:31
  • i use the above code to get the value ...but i wonder how to retrieve the value from same txt document...that is random number Commented Jan 29, 2012 at 9:31
  • 4
    I am glad you are moving your career towards programming instead of singing. I think that's the best for all. Btw, you have a typo in your last name :) Commented Jan 29, 2012 at 9:36
  • Please add this (formatted) code to your question, it'll help you get a good answer. Commented Jan 29, 2012 at 9:36

3 Answers 3

1

To write to the already existing file :

file_put_contents("file.txt","\n31231231",FILE_APPEND);

To get a random value from the file :

$file = file("file.txt")
$len = count($file);
$rand  = rand ( 0, $len-1 );
echo $file[$rand];

Edit: While retrieving the PHP_EOL is included so do this:

echo intval($file[$rand]);

Edit: Seeing as intval() returns the max_int for numbers that exceed int specs, just use trim();

echo trim($file[$rand])
Sign up to request clarification or add additional context in comments.

6 Comments

oh thank you so much..this works..how can i put the numbers in new line next time...
@Sabari OK. Replace \n with the PHP_EOLconstant. It works for me on windows though.
when i am getting back a value...it adds new line..how can i remove it
@JustinBiaber echo intval($file[$rand]);
its returning some garbage values :(
|
0

To save and access variables and information when stored as text, in db or flat files, create array of the data first, then use serialize() on the data before saving, and unserialize() after fetching again. This allows for easy handling of information.

And remember to save many smaller files , rather than a large one...

Comments

0

To write in to a file you can use :

file_put_contents("test.txt","31231231".PHP_EOL ,FILE_APPEND);

Here PHP_EOL outputs \r\n or \n depending on the OS.

To select a random value from the text file you can use:

$file = file("test.txt")
$file_length = count($file);
$random_value  = rand ( 0, $file_length-1 );
echo $file[$random_value];

New Edit:

If you want to avoid new line at the retrieved output you can do :

echo intval($file[$random_value]);

Hope this helps

2 Comments

when i am getting back a value...it adds new line..how can i remove it
Just change the last line to echo intval($file[$random_value]); You will get it correct. Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.