Inspector Mills Posted June 4, 2012 Share Posted June 4, 2012 Hi there I need a PHP script to extract a number of lines from a text file, and insert them into an array. The lines that I need to extract all have the following in common: - the line preceding it has a line of dashes, preceded by 4 spaces ; - the line after it is blank, and then is followed by another line of dashes (with no preceding spaces). Like this: ---------------------- Total : 26.225 % -------------------------- Any suggestions on how to do this? Thanks in Advance. Mills Quote Link to comment Share on other sites More sharing options...
Barand Posted June 4, 2012 Share Posted June 4, 2012 fopen() opens file fgets() reads a line of a file substr() gets a portionof a string fclose() closes the file again There's some ammo to get you started Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 4, 2012 Share Posted June 4, 2012 How big is the file and how often would this be run? The simplest solution, IMHO, is a file_get_contents() and the a preg_match_all() regex to get all those lines. But, if this is a large fine and/or would be run many times such that performance is an issue then using the other file reading functions and string functions may be more efficient. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 4, 2012 Share Posted June 4, 2012 If I was able to analyze some test files I might be able to produce a more efficient regular experssion, but based upon your explanation and example, this should work $input = file_get_contents($filePath); preg_match_all("# -*\r\n\s([^\r]*)\r\n\r\n-*#", $input, $matches); print_r($matches[1]); That will work on a windows server, for a unix server I think you need to user just "\r" instead of "\r\n" preg_match_all("# -*\r\s([^\r]*)\r\r-*#", $input, $matches); Quote Link to comment Share on other sites More sharing options...
Barand Posted June 4, 2012 Share Posted June 4, 2012 or <?php $fp = fopen('test.txt', 'r'); // open file to read $required = false; $arr = array(); while ($line = fgets($fp, 1024)) { // read each line of the file if ($required) { $arr[] = trim($line); // flagged as required, so store it $required = false; // do not want next line now } if (substr($line,0,4)==' ' && substr($line,4,4)=='----') { // is this a line before total line? $required = true; // flag that we want the next line } } // close the file fclose($fp); echo '<pre>'.print_r($arr, 1).'</pre>'; // show results ?> It would be interesting to benchmark the two approaches Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 4, 2012 Share Posted June 4, 2012 or <?php $fp = fopen('test.txt', 'r'); // open file to read $required = false; $arr = array(); while ($line = fgets($fp, 1024)) { // read each line of the file if ($required) { $arr[] = trim($line); // flagged as required, so store it $required = false; // do not want next line now } if (substr($line,0,4)==' ' && substr($line,4,4)=='----') { // is this a line before total line? $required = true; // flag that we want the next line } } // close the file fclose($fp); echo '<pre>'.print_r($arr, 1).'</pre>'; // show results ?> It would be interesting to benchmark the two approaches As I stated I would want to analyze the actual files before I set upon a final solution. But, with all due respect, your solution doesn't meet the requirements regarding the required "post" lines. You only check that the line is preceded by " -------". Based upon the actual data that might be sufficient, but I didn't want to make that assumption based upon the requirements the OP posted. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 4, 2012 Share Posted June 4, 2012 ..... then using the other file reading functions and string functions may be more efficient. I wasn't trying to slap you across the face with a gauntlet, I was merely curious. 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.