display random line from text file then delete line

specopkirbs

BANNED
Joined
Nov 28, 2008
Messages
920
Reaction score
752
im looking for the php code to display random line from text file and then delete that line from the text file
any help would be appreciated happy to pay
 
If it 's not a huge file, something like this should work

Code:
$file = "foo.txt";

$lines = file($file);

$line = mt_rand(0,count($lines)-1);
unset($lines[$line]);

$fp = fopen($file, 'w+');

foreach($lines as $line) {
    fwrite($fp,$line); 
}

fclose($fp);
 
With the echo bit added :)

Code:
$file = "foo.txt";

$lines = file($file);

$line = mt_rand(0,count($lines)-1);

echo $lines[$line];

unset($lines[$line]);

$fp = fopen($file, 'w+');

foreach($lines as $line) {
    fwrite($fp,$line); 
}

fclose($fp);
 
Back
Top