There is a file listing.txt, and in it words of this kind:

слово 1 слово 2 .... слово 4444 слово 4445 

I split it into list1.txt, list2.txt ... 400 lines each:

 <?php $lines = file("listing.txt"); $fc = 1; $lc = 400; // по сколько строк в файле $fp = fopen("list1.txt", "a"); for ($i=0; $i<count($lines); $i++) { fwrite($fp, $lines[$i]); if ($i/$lc==floor($i/$lc) and $i!=0) { fclose($fp); $fc += 1; $fp = fopen("list".$fc.".txt", "a"); } } fclose($fp); ?> 

At the end of each file there is an empty line:

 слово 1 слово 2 .... слово 399 слово 400 и тут пустая строка (401 строка) 

How to remove it by correcting this script?

  • @jikol, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

Keep the normal option

 Определяем функцию - создание порций из файла. Параметры: $filename- путь до исх файла; $sizeporcii - кол-во строк в файле порции; $mask- маска файла порции; function createporcii($filename,$sizeporcii,$mask) { $file=file($filename); $countporcii=ceil(count($file)/$sizeporcii); for ($i=0;$i<$countporcii;$i++) { file_put_contents($mask.($i+1).'.txt',array_merge(array_slice($file,$sizeporcii*$i,($sizeporcii-1)),array(trim($file[($i+1)*$sizeporcii-1])))); } } вызываем createporcii('1111_.txt',400,'list');