3

Currently I am writing urls to a txt file using the following.

$data['url'] = $element->href;  
$data['image'] = $image->content;   
$data['title'] = $title2->plaintext;    
$data['genre'] = $genre->plaintext;     
file_put_contents('done.txt', $data['url'] . PHP_EOL, FILE_APPEND);

The above is being used inside of a foreach loop, and the website I am collecting data from has 39 results per page, I am wondering how I could delete the previous 39 results on the 40th post without removing the current input flow, that way the txt file has a maximum of 39 urls in it at one time.

EDIT: Sloppy mistake on my part, sorry.

$html = file_get_html('http://oursite.com');         
foreach($html->find('.ourclass') as $element) 
{
    $data['url'] = $element->href;  
    $data['image'] = $image->content;   
    $data['title'] = $title2->plaintext;    
    $data['genre'] = $genre->plaintext;     
    file_put_contents('done.txt', $data['url'] . PHP_EOL, FILE_APPEND);
}

Above is how my foreach is laid out.

Phil
  • 157,677
  • 23
  • 242
  • 245
Land
  • 119
  • 1
  • 5
  • SimpleHTMLDom parser is a truly terrible library. I highly recommend you try out some alternatives ~ https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – Phil Oct 03 '17 at 23:07

1 Answers1

4

Change the flags argument based on the current loop iteration.

Assuming $i is the current iteration index in your foreach loop...

file_put_contents('dont.txt', $data['url'] . PHP_EOL, $i % 39 ? FILE_APPEND : 0);

This will empty the file and start writing new records every 40th iteration.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Please view my edit Phil, I've changed my structure around to match your current example and it's working nicely, but for educational purposes would love to see an answer for the other method also! – Land Oct 03 '17 at 23:01
  • `if ($i >= 39) break; // break exit loops` – β.εηοιτ.βε Oct 03 '17 at 23:04
  • @b.enoit.be I don't understand your comment. OP does not want to stop iteration at 39 (I think) – Phil Oct 03 '17 at 23:09
  • @Land I'm not sure what you mean by *"other method"* – Phil Oct 03 '17 at 23:09
  • @Phil that is how I understand the OP « other method » comment but might be wrong. That said your way of doing it, and especially the modulo usage is way more elegant. – β.εηοιτ.βε Oct 03 '17 at 23:13