joshblue Posted May 12, 2008 Share Posted May 12, 2008 How can I split the contents of the text files to multiple text files? My delimiter will be a blank line... ??? Link to comment https://forums.phpfreaks.com/topic/105254-solved-split-the-contents-of-the-text-file-to-multiple-text-files/ Share on other sites More sharing options...
Orio Posted May 12, 2008 Share Posted May 12, 2008 <?php $filename = "data.txt"; $file = file($filename); $i = 1; $current = array(); foreach($file as $line) { if(preg_match("|^\s*$|", $line) && !empty($current)) { file_put_contents($i.".txt", implode("\n", $current)); $current = array(); $i++; } else $current[] = $line; } if(!empty($current)) file_put_contents($i.".txt", implode("\n", $current)); echo "File split into {$i} files successfully"; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/105254-solved-split-the-contents-of-the-text-file-to-multiple-text-files/#findComment-538919 Share on other sites More sharing options...
joshblue Posted May 12, 2008 Author Share Posted May 12, 2008 <?php $filename = "data.txt"; $file = file($filename); $i = 1; $current = array(); foreach($file as $line) { if(preg_match("|^\s*$|", $line) && !empty($current)) { file_put_contents($i.".txt", implode("\n", $current)); $current = array(); $i++; } else $current[] = $line; } if(!empty($current)) file_put_contents($i.".txt", implode("\n", $current)); echo "File split into {$i} files successfully"; ?> Orio. It worked! Thank very much! ;D Link to comment https://forums.phpfreaks.com/topic/105254-solved-split-the-contents-of-the-text-file-to-multiple-text-files/#findComment-538938 Share on other sites More sharing options...
joshblue Posted May 12, 2008 Author Share Posted May 12, 2008 Uhm, Orio can you explain how you make it work. The logic of your codes, that is. Just put some comments on every lines And Thanks again! Link to comment https://forums.phpfreaks.com/topic/105254-solved-split-the-contents-of-the-text-file-to-multiple-text-files/#findComment-539018 Share on other sites More sharing options...
Orio Posted May 12, 2008 Share Posted May 12, 2008 <?php $filename = "data.txt"; //The data you want to split $file = file($filename); //Read the file into an array- each line as a element $i = 1; //Counter $current = array(); //Holds the lines from the previous empty-line (or file start) to the next empty-line (or file end) foreach($file as $line) //Go over each line { if(preg_match("|^\s*$|", $line) && !empty($current)) //If an empty line was found (contains only whitespaces) AND $current is not empty (we are not facing a two empty lines in a row) { file_put_contents($i.".txt", implode("\n", $current)); //Put the data in current into a file (see implode and file_put_contents in the manual //Reset the data ($current) and advance the counter $current = array(); $i++; } else $current[] = $line; //Add data (the current non empty line) to $current } if(!empty($current)) //Add the last chunk of data to a file file_put_contents($i.".txt", implode("\n", $current)); echo "File split into {$i} files successfully"; //Output... ?> Enjoy. Orio. Link to comment https://forums.phpfreaks.com/topic/105254-solved-split-the-contents-of-the-text-file-to-multiple-text-files/#findComment-539272 Share on other sites More sharing options...
joshblue Posted May 13, 2008 Author Share Posted May 13, 2008 <?php $filename = "data.txt"; //The data you want to split $file = file($filename); //Read the file into an array- each line as a element $i = 1; //Counter $current = array(); //Holds the lines from the previous empty-line (or file start) to the next empty-line (or file end) foreach($file as $line) //Go over each line { if(preg_match("|^\s*$|", $line) && !empty($current)) //If an empty line was found (contains only whitespaces) AND $current is not empty (we are not facing a two empty lines in a row) { file_put_contents($i.".txt", implode("\n", $current)); //Put the data in current into a file (see implode and file_put_contents in the manual //Reset the data ($current) and advance the counter $current = array(); $i++; } else $current[] = $line; //Add data (the current non empty line) to $current } if(!empty($current)) //Add the last chunk of data to a file file_put_contents($i.".txt", implode("\n", $current)); echo "File split into {$i} files successfully"; //Output... ?> Enjoy. Orio. THANK YOU VERY MUCH! Link to comment https://forums.phpfreaks.com/topic/105254-solved-split-the-contents-of-the-text-file-to-multiple-text-files/#findComment-539648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.