Jump to content

Hard PHP Question, regarding files


Rifts

Recommended Posts

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.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/209752-hard-php-question-regarding-files/
Share on other sites

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.

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.