0

So here is basically what i want to do, say var.txt is = :

bar
foo
smooth
green

then say i run a code like this in php to grab a random line:

$mineloc = file("var.txt");
$mineacc = $mineloc[array_rand($mineloc)];

what would i do then to remove the line that shows up in $mineacc from var.txt which will result in var.txt being this if the word smooth was generated:

bar
foo
green
  • Does this answer your question? [how to delete a single line in a txt file with php](https://stackoverflow.com/questions/48490252/how-to-delete-a-single-line-in-a-txt-file-with-php) – domsson Jan 19 '20 at 09:07
  • Naive approach: read every line from file, then overwrite the file with the lines you read, minus the one that you picked. See the possible duplicate question for more details. – domsson Jan 19 '20 at 09:09

1 Answers1

2

I hope this will solve your problem....

$content = file_get_contents('var.txt');
$contentArray = explode('
',$content);

unset($contentArray[rand(0, count($contentArray))]);

file_put_contents('var.txt', implode('
',array_values($contentArray)));
Ronak Dhoot
  • 2,322
  • 1
  • 12
  • 19