Add a fixed line to every 50 PHP CSV lines

Asked

Viewed 567 times

1

I have a problem. I have a CSV file with 490 lines (on average this value can vary) and I would like to add 1 certain number to 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 line divider in CSV is the comma).

Example:

1122222222
1199999999
1234567890

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

9977777777

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

This number are verification codes that must be done every 50 numbers in order for a system to function properly.

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

Because in case I can’t do that, I have a script that is splitting a 65000 line file into 490 lines. Maybe it’s more cool to add the fixed numbers in this big file and then have them split into 500 lines. What do you say?

Something else, I would need that if the option is to use the 490-line file, it looks for ALL csv files in the folder (maybe using the function: glob('*.CSV') followed by a foreach) as the 65000 line file was split into several with 490. If done in the file larger than 65000 lines, this loop will not be necessary.

Would it be possible to do this in PHP? *** This has to have an output 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!";
?>

2 answers

1

In the generation of your CSV, in the looping part you can make the calculation 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 line to every 5 records (in your case line in CSV)

Tailoring 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
}
  • It would not be correct if($i%50) == 0 ?

  • html tags in csv? I don’t think it’s going to work... Do you think you could adapt that to my reality? Besides, that way he won’t add the number I need to the first line. It should be added to EVERY 50 lines and also in the first line of the file.

  • Yes, just change this value to 50, I put 5 just to exemplify with a smaller number of lines

  • The add line code in your CSV is just put where this echo, it will always add in the first line, 0 % 50 == 0

  • And in this part '$i< 100' instead of 100 would you put 490 or 500? And how would you like to add in the FIRST line the number I need? Now that I remembered: This solution would not work to add line in the file, it would just give a "print" on the screen. I need you to rewrite the csv file.

  • I added in the answer more or less how it would look in your code, the variable $totalRegistro is the amount of record you intend to add in CSV

  • Yes... but as I said, PHP will not save the echo result in CSV. I’ll edit the question with the script I’m using to split the large file.

  • You can use the Phpexcel to read excel and add lines and generate a new CSV, or with the fwrite add content, style what you are doing

  • But that would be an extra library and a script that could be simple, will be even heavier. Could it be that for a simple problem like this there is no php library to manage it in a simplified way? Your idea is cool, too bad you didn’t save in CSV....

Show 4 more comments

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!";
?>  
  • @Jeferson thus adds 1 number to every 50 lines "approximately". He adds on line 1, on line 52, line 103, line 154... I don’t know why the last digit is changing... It shouldn’t be 1~50~100~150...?

  • It would have to do every 49 then, for the inserted line to be 50 right...

  • I don’t know for sure... but that way you already answered me.

  • Well, if you want 50 to 50, you gotta be $i%49.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.