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!";
?>