Jump to content

exctract entire lines from text file to array


Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

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.