Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a line from a text file, then delete it

Tags:

php

I have a text file , filled with pipe separated URLs, like this:

http://www.example.com|http://www.example2.com|http://www.example3.com|.... 

etc.

I currently have this code to read each url from the file and pass it to a function:

<?php
  $urlarray = explode("|", file_get_contents('urls.txt'));

  foreach ($urlarray as $url) {

   function1($url);

   }

?>   

What I want to do next is, after function1() is done, delete that URL from the text file.That way, when the script is done going through all the URLs in urls.txt, this text file should be empty.

How can I achieve this?

Thanks, Rafael

like image 354
RafaelM Avatar asked Oct 18 '25 15:10

RafaelM


1 Answers

There's no way to delete stuff from the beginning or middle of a text file without rewriting the whole thing. See here. Just go through all the URLs and then delete the file, or implode() any remaining URLs and overwrite the original file with it.

like image 112
kijin Avatar answered Oct 21 '25 06:10

kijin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!