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!";
?>
It would not be correct if($i%50) == 0 ?
– user28595
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.
– Diego
Yes, just change this value to 50, I put 5 just to exemplify with a smaller number of lines
– Jeferson Assis
The add line code in your CSV is just put where this
echo
, it will always add in the first line, 0 % 50 == 0– Jeferson Assis
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.
– Diego
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– Jeferson Assis
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.
– Diego
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– Jeferson Assis
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....
– Diego