young_coder Posted May 16, 2010 Share Posted May 16, 2010 Dear All, I would like to split a TXT file into multiple TXT files based on the number of lines (every line to be content for one new file, 100 lines = 100 files with one line, so every would be named like "1.txt", "2.txt", "3.txt" etc.). Thank you very much Link to comment https://forums.phpfreaks.com/topic/201967-splitting-a-txt-file-into-multiple-txt-files/ Share on other sites More sharing options...
kenrbnsn Posted May 16, 2010 Share Posted May 16, 2010 Read the whole file into an array. Loop through the array writing a new file for each line: <?php $old_file = file('old_file.txt'); $cnt = count($old_file); for ($i=0;$i<$cnt;++$i) { $n = $i + 1; file_put_contents($n . '.txt',$old_file[$i]; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/201967-splitting-a-txt-file-into-multiple-txt-files/#findComment-1059192 Share on other sites More sharing options...
young_coder Posted May 16, 2010 Author Share Posted May 16, 2010 Beautiful.. it's working like a charm… thanks Ken Link to comment https://forums.phpfreaks.com/topic/201967-splitting-a-txt-file-into-multiple-txt-files/#findComment-1059198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.