Test if unique ID is in log file

trevor1940
trevor1940 used Ask the Experts™
on
I have a log file something like bellow

How do I test if an ID exist and type = ABC?

log.txt

ID|TYPE|DATE
123|XYZ|2017-03-07
123|ABC|2017-03-08
124|XYZ|2017-03-08

Open in new window


PHP

<?php
error_reporting(E_ALL);
$Data = explode('|', file_get_contents("log.txt"));
trace($Data);
$ID = 123;
if($Data[$ID]){
  echo "found $ID";
  }

function trace($arr){
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}//end trace

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
If:
#1. It's a small file
#2. It's always pipe-delimited, with no quoted values
#3. The order is identical to what you have (ID, then Type)

...then the fastest way would be to do this:
$matched = (strpos(file_get_contents("log.txt"),"123|ABC") !== false); // $matched will be TRUE if it finds that combination of ID and Type

// Check the results
if($matched)
{
  echo "Found that combination of ID and Type!";
}
else
{
  echo "Didn't find that combination of ID and Type!";
}

Open in new window


If it's a big file, but the rest of the criteria is true (pipe-delimited, no quoted values, consistent order of columns), you can use fopen/fread:
$fp = fopen("log.txt","r");
while(!feof($fp))
{
  $line = fgets($fp);
  $matched = (strpos($line,"123|ABC") !== false);
  if($matched) { break; } // Don't bother searching the rest of the file if we already have a match
}
fclose($fp);

// Check the results
if($matched)
{
  echo "Found that combination of ID and Type!";
}
else
{
  echo "Didn't find that combination of ID and Type!";
}

Open in new window


If there is a chance of having quoted values, then you should use fgetcsv:
$fp = fopen("log.txt","r");
while(!feof($fp))
{
  $line = fgetcsv($fp,0,"|");
  $matched = (($line[0] == 123) && ($line[1] == "ABC"));
  if($matched) { break; } // Don't bother searching the rest of the file if we already have a match
}
fclose($fp);

// Check the results
if($matched)
{
  echo "Found that combination of ID and Type!";
}
else
{
  echo "Didn't find that combination of ID and Type!";
}

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
Maybe consider setting up the log file as a JSON string, so you can turn it into an object and use SimpleXML.

Please see https://iconoun.com/demo/temp_trevor1940.php
<?php // demo/temp_trevor1940.php
/**
 * https://www.experts-exchange.com/questions/29007662/Test-if-unique-ID-is-in-log-file.html
 */
error_reporting(E_ALL);

// A LOG FILE, AS IF READ BY file_get_contents()
$log = <<<EOD
ID|TYPE|DATE
123|XYZ|2017-03-07
123|ABC|2017-03-08
124|XYZ|2017-03-08
EOD;

// MAKE AN ARRAY OF ROWS
$arr = explode(PHP_EOL, $log);

// GET THE KEYS
$top = $arr[0];
unset($arr[0]);
$top = explode('|', $top);

// ASSOCIATE KEYS WITH VALUES
foreach ($arr as $line)
{
    $line = explode('|', $line);
    $data = array_combine($top, $line);

    // TEST
    if ($data['TYPE'] == 'ABC') print_r($data);
}

Open in new window

Outputs:
Array ( [ID] => 123 [TYPE] => ABC [DATE] => 2017-03-08 )

Open in new window

Author

Commented:
I hadn't realized the size of the file mattered

The log file currently doesn't exist so can be in what  ever format I want but is likely to grow daily as each time the application is run new lines will be created maybe 10 per day.

Maybe I need further explanation

each time it's run a directory is read with 1 or more files in
within each file a ID and TYPE is read
I need to test it hasn't been processed  before potentially the same ID can be used up to 3 times depending on type  however the vast majority will be once

so I would open the log file once and as I read the directory test if that file ID / TYPE has been processed before if not then process the file
Most Valuable Expert 2011
Top Expert 2016

Commented:
"Size matters" within some broad constraints.  When we're talking about adding 10 lines a day to a file, it will take years before size matters!  Any of the solutions shown here will probably be OK, but as usual, the devil is in the details.  If the test case we worked with is not truly representative of the live data, then there may be additional considerations and changes needed.

Author

Commented:
Thanx for your help and info

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial