Friends! The essence of the question is this: every 2 minutes, a txt file arrives on the server in the files directory. My task is to process data from this file. Unfortunately, I haven’t had a deal with the files yet, so I don’t know how to work with them correctly, but the deadlines for this assignment are very tight, so you need to start processing it the sooner the better. And most importantly - right.

Here's what's inside the file:

1;i;iazs-v1.1.82 2;P;1;151587;202741;294725;0;0 3;R;1;0;6588;2503;0;7436;1862;0;0 4;R;2;0;5769;2065;-6;7533;1556;0;0 5;R;3;0;15586;7730;-2;8397;6491;0;0 6;T;1;18.04.2016 09:14:08;9;000000000000;255;777;18859;1;1;0;000000000000 7;T;2;18.04.2016 09:14:08;1;00000073487F;255;777;0;1;1;1;000000000000 8;T;3;18.04.2016 09:14:26;9;000000000000;255;777;26000;1;2;0;000000000000 9;T;4;18.04.2016 09:14:26;1;00000073487F;255;777;0;1;2;1;000000000000 10;# 

Arriving files are always the same, that is, according to the rules of creation, and the number of parameters.

This is what it means: The first digit of a line is a pure line number. Next after ";" This is a data type. They are: P, R, T. The data type i does not need to be processed, this is the firmware version. Each of these data types in a string has parameters that are separated, and their number is always the same.

I, unfortunately, do not know how to implement such a parser, but I imagine it this way: 1) Open the file, 2) We get the data into a loop that ends at the end of the file. 3) Take the string, take the second element - the data type, and then do, for example, switch , with a set of functions depending on the data type. Then I will work with the array in these functions and process the data.

The whole question essentially boils down to this structuring :))

    3 answers 3

    What about using the str-getcsv or fgetcsv function ?

    Example of use:

     <?php $row = 1; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); } ?> 

    Is this suitable for txt?

    The main thing is not the file extension, but the content. It is clearly visible here that this is a CSV file with a separator ;

    and what does while-1000 mean ???

    The above are links to functions that may suit you. If you follow the links, you can look at the description of the parameters and see that 1000 is the length of the string. It has become optional in PHP 5. If this argument is not specified (or equal to 0 in versions of PHP 5.0.4 and higher), the maximum length of the string is not limited, and the function works a little slower.

    I need the most important thing is to call the data processing functions depending on which type

    Well, instead of for ($c=0; $c < $num; $c++) refer to a specific element of the array to read the type and call the function with the transfer of the remaining data. Or write your own implementation of line reading and processing.

    • Is this suitable for txt? I unfortunately can not change the file format, it sends a txt file equipment - WhoIsDT
    • and what means while (($ data = fgetcsv ($ handle, 1000, ";")) - 1000 ??? like 1000 lines? but I brought only a fragment of the file into the prier. It can be 1000 lines and more - WhoIsDT
    • Sorry but this is not very convenient. I need the most important thing is to call the data processing functions depending on what type of data we are currently parsing. therefore, I need to clearly identify what kind of data it is, call a function depending on this data type, and pass all the parameters of this string to it for processing - WhoIsDT
    • @iSeeDev added a description. - androschuk
    • Thank you, I already realized what I wanted! Your decision helped me. thank! - WhoIsDT

    $file = file_get_contents("Путь к файлу");

    Next, pull out all you need through the regulars.

    For example:

    preg_match("#\d+;[^i];(.*)", $file, $q);

       $lines = file('имя_файла'); // Получаем все содержимое файла в массив foreach ($lines as $line_num => $line) { $data=explode(';', $line); // Разбиваем строку на массив колонок if($data[1]=='i') continue; // Не обрабатывать строки 'i' ... }