Jump to content

Search text file


muppet77

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/284178-search-text-file/#findComment-1459596
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
https://forums.phpfreaks.com/topic/284178-search-text-file/#findComment-1459610
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.