SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    exctract entire lines from text file to array

    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

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi
    An ideea is to use the file('file.txt') function (see on php.net ). It creates an array with the rows from a file.
    Then you can traverse the array (with for() ), and apply some instructions, like preg_match() to store in another array the lines you want.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you don't have a rather large file, preg match will do what you want:


    Data.txt:
    Code:
    fsaffjsf some unrelated data
    sdsds
    sgfag
    238123942 
    ------
    
        ----------------------
        Total     :   26.225 %
    
    --------------------------
    dsd
    sdaffgasf35515
    2------------
    23----
        ---------------------------
        Monkeys  : Furry
    
    --------------------
    
    sds
    afaf
    PHP Code:
    $Content file_get_content('Data.txt');
    preg_match_all('~\s{4}[\r\n-]+\s*(.+):(.+)\s*[\r\n-]+~m'$Content$Matches);
    $Data = array();
    for(
    $i 0$i count($Matches[1]); $i++){
        
    $Field trim($Matches[1][$i]);
        
    $Value trim($Matches[2][$i]);
        
    $Data[$Field] = $Value;
    }
    var_dump($Data); 
    Output:
    Code:
    array(2) {
      ["Total"]=>
      string(8) "26.225 %"
      ["Monkeys"]=>
      string(5) "Furry"
    }


    If you just want an array of matching lines and no parsing the data, then:
    PHP Code:
    preg_match_all('~\s{4}[\r\n-]+\s*(.+)\s*[\r\n-]+~m'$Content$Matches);
    $Data = array();
    foreach(
    $Matches[1] as $Match){
        
    $Data[] = trim($Match);

    Output:
    Code:
    array(2) {
      [0]=>
      string(22) "Total     :   26.225 %"
      [1]=>
      string(16) "Monkeys  : Furry"
    }
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you do have a large file then best to look for start of pattern and then extract the line.

    Here is an example:
    PHP Code:
    <?php
       $pattern 
    "    ----------------------";
       
    $data = array();
       
    $extract 0;
       
    $file fopen('data.txt''r');
       while (!
    feof($file))
       {
           
    $line rtrim(fgets($file)); // remove any spaces or NL
           
    if($extract)
           {
               
    $data[] = $line;
               
    $extract 0;
           }
           if(
    $line==$pattern$extract 1;
       }
       
    fclose($file);
       echo 
    "<pre>";
       
    var_dump($data);
    ?>
    I'm using the same file as Jake Arkinstall's but modified the start of pattern string to be the same.

    Here is the result:
    Code:
    array(2) {
      [0]=>
      string(26) "    Total     :   26.225 %"
      [1]=>
      string(20) "    Monkeys  : Furry"
    }

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    My thoughts exactly Although I wasn't sure on whether or not the start would be uniform, so kinda just made it arbitrary. It's a strange format of data file, I must say.

    Out of interest, why did you opt for $extract being an integer rather than a boolean?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If start pattern could have different formats then we can use preg_match() as you did to check for various pattern that can be used.

    Although the example can use boolean, if $extract is true then extract the line and reset it back to false. However, if the match requirement is changed, say you want to extract all the lines till end pattern is found, which might expand more than one lines, then we need to set it to some number telling us whether or not the line is to be exacted. There might be some other conditions where $extract need to assign a different value to take on an appropriated action. Hope this make sense.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •