Search code examples
phpfileheaderflat

Loop for reading txt file with multiple headers and detail items in php


I have the following incoming txt file:

H header1 yyyy
I detailofheader1
I detailofheader1
H header2 xxxx
I detailofheader2
I detailofheader2

And the following code:

$action = substr($line_of_text, 0, 1);
if (($action == 'H') )  {
//STORE IN VARIABLES ;
}
if (($action == 'I') )  {
//store in variables and upload header and detail variables to mysql;
}

I would like to have a loop read first header store in variable then read details and everytime it hits 'I' it uploads that line to MYSQL.

How do i go back to the top of the loop once it hits H again?

Thanks from a noobie.


Solution

  • I would like to have a loop read first header store in variable then read details and everytime it hits 'I' it uploads that line to MYSQL.

    There are multiple ways to parse your file.

    The following approach makes use of explode() for H and I.

    So you have a "outer loop" for H (H header1 yyyy I detailofheader1 I detailofheader1) - to process the individual headers.

    And an "inner loop" for I (header1 yyyy I detailofheader1 I detailofheader1) - to process the header data itself and the details.

    SQL-wise:

    • I don't know your SQL schema - just guessing around on the schema.
    • Where i use echo $sql - you could exec the query.
    • this is example SQL, escape it properly to make it safe

    Anyway, here's something to play around with. Hope that gets you started...

    <?php
    
    // example data to work with
    $string = 'H header1 yyyy I detailofheader1 I detailofheader1 H header2 xxxx I detailofheader2 I detailofheader2';
    
    // later just
    // $string = file("file.txt");
    
    // get rid of the first H (to ease exploding / no empty first array item)
    $string =  ltrim($string, 'H');
    
    // explode string into array - by using H as char for the split
    $headers = explode('H', $string);
    
    foreach($headers as $idx => $line)
    {
        $line = trim($line); // remove spaces at begin/end
    
        //echo 'Processing: [' . $idx . '] ' . $line . PHP_EOL;
    
        // second explode, this time using "I"
        $data = explode('I', $line);
    
        foreach($data as $dataIdx => $details)
        {
            $details = trim($details); // remove spaces at begin/end
    
            //echo '           ' . $details . PHP_EOL;
    
            // first dataIdx is the header data "header1 yyyy"
            if($dataIdx === 0) {
                // string contains a space, split
                $headerItems = explode(' ', $details);
                $headerNumber = trim($headerItems[0]); // header1
                $headerString = trim($headerItems[1]); // yyyy`
    
                // => insert the header
    
                $sql = "INSERT INTO table_headers (id, name) VALUES ('$headerNumber',' $headerString')";
                echo $sql . PHP_EOL;
    
                // we are done, skip to next element
                continue;
            } 
    
            // else its a "detailofheaderX"
            // so use details in the insert query
    
            $sql = "INSERT INTO table_data (header_id, detail) VALUES ('$headerNumber', '$details')";
            echo $sql . PHP_EOL;
        }
    }