Rifts Posted August 4, 2010 Share Posted August 4, 2010 Please delete my other post as I realized it is not what I needed and I cannot delete it myself. Here is exactly what I need. I have X amount of text files (with random names) in each text file there is Y amount of lines with one word on each line. I need to open all X amount of files and take the first line from each and save it in a new txt doc. then take the second line of each and save them to a new text doc. and so on. so I have all the line 1's from all the txt files together and all the line 2's together etc. I hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/209752-hard-php-question-regarding-files/ Share on other sites More sharing options...
Adam Posted August 4, 2010 Share Posted August 4, 2010 This should be pretty simple to do. I'm not sure of the file structure though so I can only suggest how to obtain the files; scandir will allow you scan through all files in a directory, and glob would allow you to match specific files (i.e. all "*.txt" files) and loop through them. Once you're looping through your file list, open each file with the file function which will return the contents as an array (split by line). You can then loop through each line, storing the value (plus "\n" for a new line) to the correct text file. Quote Link to comment https://forums.phpfreaks.com/topic/209752-hard-php-question-regarding-files/#findComment-1094981 Share on other sites More sharing options...
Mchl Posted August 4, 2010 Share Posted August 4, 2010 DirectoryIterator to iterate over files Open first file, save first line to file 1, second to file 2, third to 3 etc... Open second file, save first line to file 1, second to file 2, third to 3 etc... Repeat until there are no more files. Quote Link to comment https://forums.phpfreaks.com/topic/209752-hard-php-question-regarding-files/#findComment-1094982 Share on other sites More sharing options...
Rifts Posted August 4, 2010 Author Share Posted August 4, 2010 ahhh thank you both for the answers they sound like they are exactly what I need. I'm no pro at php so I could use a little more help. the file structures will all be the same and its really simple. they will all have the exact same amount of lines. and each line will have a 12 letter word on them also there will never be a duplicate. Thanks a lot I appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/209752-hard-php-question-regarding-files/#findComment-1095177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.