Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!
Content of the text file (test.txt):

115.5.108.249 2015-03-01
118.110.6.87 2018-01-03
36.120.105.13 2018-04-06

I'm familiar with the following:

<?php 
    $fname = "test.txt";
    $date = date("Y-m-d");
    foreach($lines as $line) if(!strstr($line, $date)) $out .= $line;  
    $f = fopen($fname, "w");  
    fwrite($f, $out);  
    fclose($f);
?> 

This command will only delete the line that contains the current server history and date ($date). I want this command to run in reverse. Line cleaner whose history (date) is old and no match with current server date (The server where PHP runs). I hope you understand what I mean.
Thanks.


What I have tried:

<?php
$fname = "test.txt";
$date = date("Y-m-d");
foreach($lines as $line) if(!strstr($line, $date)) $out .= $line;
$f = fopen($fname, "w");
fwrite($f, $out);
fclose($f);
?>
Posted
Updated 5-Apr-18 23:17pm

1 solution

To reverse the operation just reverse the check.

Because strstr is slow and memory consuming, PHP: strpos - Manual[^] would be better:
PHP
foreach($lines as $line) if(strpos($line, $date) !== false) $out .= $line;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900