Add a fixed line every 50 PHP CSV lines

1

I have a problem. I have a CSV file with 490 lines (on average, this value may vary) and would like to add 1 certain number every 50 lines.

Each line basically consists of 10 numeric digits and after these digits, a new line is created (if I'm not mistaken, the CSV line splitter is the comma).

Example:

1122222222
1199999999
1234567890

I would like the following number to be added on line 1 / line 50 / line 100 / line 150:

9977777777

This number would be the same on all of these new rows. Taking into account that I would create 1 number equal to every 50 lines (starting from the first), I would have 10 new numbers.

This number is a verification code that should be done every 50 numbers in order for a system to work properly.

The question at the end of this would be: Would this resulting file have 500 lines (10 being the number added)?

If I can not do this, I have a script that is dividing an entire file of 65000 lines into 490 lines. Maybe it's cool to add the fixed numbers to this big file and then have them split into 500 lines. What do you say?

Another thing would be that if you use the 490-line file, it will search ALL csv files in the folder (perhaps using the function: glob('*.CSV') followed by a foreach) since the 65000 line file was I divide it into several with 490. If it is done in the file larger than 65000 lines, you will not need this loop.

Could it be possible to do this in PHP? *** This has to have an output on file. That is, either rewrite the CSV file or create a new one.

DIVISION SCRIPT:

<?php
$name = "bh";
$bigFile = fopen("$name.csv", "r");
$j = 0;

$content = file_get_contents("$name.csv");
$content = str_replace(';', '', $content);
file_put_contents("$name.csv", $content);

while(! feof($bigFile)) {
    $smallFile = fopen("$name $j.csv", "w");
    $j++;

    for ($i = 0; $i < 490 && ! feof($bigFile); $i++) {
        fwrite($smallFile, fgets($bigFile));

    }
    fclose($smallFile);

}
fclose($bigFile);

echo "Pronto!";
?>
    
asked by anonymous 14.10.2015 / 17:00

2 answers

0

Based on the idea of @Jeferson Assis I was able to adapt my script to:

<?php
$name = "bh";
$bigFile = fopen("$name.csv", "r");
$j = 0;

$content = file_get_contents("$name.csv");
$content = str_replace(';', '', $content);
file_put_contents("$name.csv", $content);

while(! feof($bigFile)) {
    $smallFile = fopen("$name $j.csv", "w");
    $j++;

    for ($i = 0; $i < 500 && ! feof($bigFile); $i++) {
        if($i%50 == 0){
            fwrite($smallFile, "9977777777");
            fwrite($smallFile, PHP_EOL);
        }
        fwrite($smallFile, fgets($bigFile));

    }
    fclose($smallFile);

}
fclose($bigFile);

echo "Pronto!";
?>  
    
15.10.2015 / 15:21
1

In the generation of your CSV, in the part of the looping you can do the calculate the rest of 50.

Example:

for($i = 0; $i< 100; $i++){
    if($i%5 == 0){
        echo '<hr />';
    }
    echo $i.'<br />';   
}

In the example it adds one row to every 5 records (in your case row in CSV)

Fitting the code to generate your excel:

$totalRegistro = 100;

for($i = 0; $i< $totalRegistro; $i++){
    if($i%50 == 0){
        //Aqui vai a parte onde você adiciona a linha com o valor 9977777777
    }

    //Aqui você adiciona o valor normalmente na sua planilha
}
    
14.10.2015 / 17:14