开发者

Break large .csv into smaller .csvs with proper header field with PHP

We have a little program we use internally to upload lists into our system. It won't take file开发者_StackOverflow社区s longer then 5k lines, and needs each .csv to have the same formatting, including the header row from the original file.

Could someone point me in the direction of a script I could use to accomplish this on a server running PHP?

Thanks!


Do these csvs contain line breaks in their values? If not, it is easy to read the first line (the header) and put groups consequent lines in separate files preceeded by that first line. You can read a file line by line using the fgets function.

If the values can contain breaks, you will need to actually parse the csv or find a library that can do that for you. This is still not that hard (depending on the exact format and escaping of special characters. A common approach is to include values in " when they contain special characters. The quotes themselves are escaped by duplicating them. In that case it is still simple to split the file: You will need to read line by line. If a line contains an odd number of lines, the line ends with an open value, so you will need to read the next line and add it to the line just read. Repeat this until you find another line with an odd number of quotes.

This way, you can easily split the files without actually having to parse them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜