Jump to content

Search text file


muppet77

Recommended Posts

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 by muppet77
Link to comment
Share on other sites

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 by Irate
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.