phptextfeof

read text from file via .php and store parts in variable


Problem is reading text from file and prepare and save specific text to variables for insert into database. Something like here: https://www.wdb24.com/php-read-text-file-and-insert-into-mysql-database/

Measurment/Import data looks like:

    1=19-10-18 10:02:06
    2=+1.313026E+00 l/s 
    3=+1.671796E-01m/s
    4=+1.500691E+02m3 
    5=+1.501138E+02m3 
    6=+0.000000E+00m3 
    1=19-10-18 10:03:06
    2=+1.266786E+00 l/s 
    3=+1.612923E-01m/s
    4=+1.501403E+02m3 
    5=+1.501850E+02m3 
    6=+0.000000E+00m3 
    1=19-10-18 10:04:06
    2=+1.597391E+00 l/s 
    3=+2.033861E-01m/s
    4=+1.502291E+02m3 
    5=+1.502738E+02m3 
    6=+0.000000E+00m3
     .
     .
     . 

Always only the first three rows (1=...2=...3=...) from six repeatedly rows need to store in variable like $first, $second, $third. Like array maybe, because I need to save text data in ONE FIELD in database

$first=19-10-18 10:02:06,19-10-18 10:03:06,19-10-18 10:04:06 //from 1=

$second=+1.313026E+00,+2.333026E+00,+2.123026E+00 //from 2=

$third=+1.671796E-01,1.87794E-01,1.34146E-01 //from 3=

I have small part of code, please someone help with preparing data (inserting in database is not problem).

    <?php

$file=fopen("test_file/test.txt", "r");

while(!feof($file)){

    $content=fgets($file);
    $content=explode(PHP_EOL,$content);  //break line?
    //list($first,$second,$third,$four,$five,$six)=$content;
    var_dump ($content); //result is some strange array, can't call $content[0] in some loop...


}


fclose($file);

?>

Result is some strange looking arrray or not? I am php beginer, thanks in advance for any suggestion, link...

If you wonder what is the end product, it's google chart that reads data from database. I think it's inconvenient to have table for every measurment and plan is to store it in like:

id | timestamps | values1 | values2

1 | many times | manyValues | manyValues

2 ............

So in this case there is only one tabel with many measurments.


Solution

  • Your code would look like something like this:

    <?php
    
    $handle = fopen("inputfile.txt", "r");
    $lineVariables = [];
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            $tmp = explode('=', $line);
            $lineVariables[trim($tmp[0])][] = trim($tmp[1]);
        }
        fclose($handle);
    
        $result = [];
        foreach ($lineVariables as $key => $variable) {
            $result[$key] = implode(', ', $variable);
        }
    
        echo '<pre>' . print_r($result, true) . '</pre>';
    } else {
        // error opening the file.
    }
    

    Output:

    Array
    (
        [1] => 19-10-18 10:02:06, 19-10-18 10:03:06, 19-10-18 10:04:06
        [2] => +1.313026E+00 l/s, +1.266786E+00 l/s, +1.597391E+00 l/s
        [3] => +1.671796E-01m/s, +1.612923E-01m/s, +2.033861E-01m/s
        [4] => +1.500691E+02m3, +1.501403E+02m3, +1.502291E+02m3
        [5] => +1.501138E+02m3, +1.501850E+02m3, +1.502738E+02m3
        [6] => +0.000000E+00m3, +0.000000E+00m3, +0.000000E+00m3
    )
    

    Explanation

    In the while cycle you read the lines with this:

    $line = fgets($handle)
    

    Then you need to process these lines. Since the index of the variables is the first character separated from the line variables you would like to add to the lines, you can use explode function using the '=' character as a delimiter.

    Then in the $tmp array you will have 2 elements: $tmp[0] will contain the indexes of the result. And $tmp[1] will contain the rest of the line.

    For example the first line:

    $tmp[0] // contains 1
    $tmp[1] // contains 19-10-18 10:02:06
    

    After the while cycle, our $lineVariables will look like this:

    Array
    (
    [1] => Array
        (
            [0] => 19-10-18 10:02:06
            [1] => 19-10-18 10:03:06
            [2] => 19-10-18 10:04:06
        )
    
    [2] => Array
        (
            [0] => +1.313026E+00 l/s
            [1] => +1.266786E+00 l/s
            [2] => +1.597391E+00 l/s
        )
    
    [3] => Array
        (
            [0] => +1.671796E-01m/s
            [1] => +1.612923E-01m/s
            [2] => +2.033861E-01m/s
        )
    
    [4] => Array
        (
            [0] => +1.500691E+02m3
            [1] => +1.501403E+02m3
            [2] => +1.502291E+02m3
        )
    
    [5] => Array
        (
            [0] => +1.501138E+02m3
            [1] => +1.501850E+02m3
            [2] => +1.502738E+02m3
        )
    
    [6] => Array
        (
            [0] => +0.000000E+00m3
            [1] => +0.000000E+00m3
            [2] => +0.000000E+00m3
        )
    
    )
    

    Now you can close the file and focus on formating the result to the desired look. We are going to store the result in the $result variable, which is an array, and one element of the array will represent one desired result, and the indexes on the array will be the reference to the first number character of the lines.

    To append the elements of the $lineVariable for each line, we can use the implode function, which is kind of the opposite of the explode function. For one single this will convert this:

    Array
        (
            [0] => 19-10-18 10:02:06
            [1] => 19-10-18 10:03:06
            [2] => 19-10-18 10:04:06
        )
    

    Into this one single line of string:

    19-10-18 10:02:06, 19-10-18 10:03:06, 19-10-18 10:04:06
    

    So we just go through on our prepared $lineVariables and process the elements to the desired format.

    That's it.