I have a large text file around 25k lines... I need to divide them into 2000 and put those each 2000 into text file...code i have so far.
<?php
$lines = file("largefile.txt", FILE_IGNORE_NEW_LINES);
$i = 1;
foreach($lines as $lol)
{
//now i need to put first 2000 lines in 1.txt and then next 2000 lines in 2.txt
file_put_contents($i".txt", $lol . PHP_EOL, FILE_APPEND)
}
?>
now i need to put first 2000 lines in 1.txt and then next 2000 lines in 2.txt and so on..
I think you can loop in your lines array and count your loop, so you can change filename according with your total required lines as follow
$i=1;
$o=0;
for($u=0;$u<count($lines);$u++)
{
if($o<2000)
{
file_put_contents($i".txt", $lines[$u] . PHP_EOL, FILE_APPEND);
$o++;
} else {
$i++;
file_put_contents($i".txt", $lines[$u] . PHP_EOL, FILE_APPEND);
$o=1;
}
}
Thanks. I think you can use the linux command split like this.
<?php
exec("split largefile.txt -l 2000");
?>