muppet77 Posted November 22, 2013 Share Posted November 22, 2013 (edited) I would like to search a text file for a date and then stop searching when the date changes. Eg search for 19-10 18-10 monkey 19-10 bananas 19-10 nuts 20-10 leaves Would return 19-10 bananas 19-10 nuts Could anyone please help me make some sort of loop(?) script? Thanks Edited November 22, 2013 by muppet77 Quote Link to comment Share on other sites More sharing options...
Irate Posted November 22, 2013 Share Posted November 22, 2013 (edited) Assuming we have a file called "date.txt" in the root directory of your filesystem. <?php $txt = file_get_contents("http://yourdomain.com/date.txt"); $lines = explode("\n",$txt); $retVal = ""; foreach($lines as $line) { if(preg_match("@(\d{2}\-\d{2}(.*?)@",$line)&&(substr($retVal,0,4)!=substr($line,0,4)) $retVal .= substr($line,0) . "\n"; else continue; } echo printf("<pre>%1</pre>",$retVal); ?> It's not been tested, though it should work. Edit: added a minor thing. Edited November 22, 2013 by Irate Quote Link to comment Share on other sites More sharing options...
muppet77 Posted November 22, 2013 Author Share Posted November 22, 2013 (edited) Thanks. Where do I put the date please? With a second look it is in the format 19-02-2009 Edited November 22, 2013 by muppet77 Quote Link to comment Share on other sites More sharing options...
Irate Posted November 22, 2013 Share Posted November 22, 2013 Ah. Adjust this line, then. if(preg_match("@([0-3]{1}[0-9]{1}\-[0-1]{1}[0-9]{1}\-[1-2]{1}[0-9]{3})@",$line)&&(strlen($retVal)<9)&&(substr($retVal,0,9)!=substr($line,0,9))) $retVal .= $line . "\n";I'm not particularly sure about this, but this should theoretically work. Also, remove the echo before the printf function. Quote Link to comment Share on other sites More sharing options...
muppet77 Posted November 23, 2013 Author Share Posted November 23, 2013 Thanks. This seems to keep searching the entire file which is huge. Is there another way? The days are in order so once the day changes there will be no others later in the file. Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.