Last Updated: February 25, 2016
· antjanus

Parsing CSV files in PHP (the fast way)

I needed to parse a CSV file extremely efficiently today. With a thousand rows and fifty columns, this is no easy task.

Here's where I went wrong in my approach and where you should learn a lesson. I used the OOP approach. I found a robust AIO swiss-knife library that could parse CSVs, all kinds of Excel files, and more. The result? 5 second load and adding any logic to the parsing would crash the script.

Better approach? I found this cool post on PHP CSV parsing:

and what did I learn? That a 5 second load could be cut down to 500 or less milliseconds and I could add some basic logic to the parsing without slowing it down. The snippet uses PHP's built-in functions which is probably what makes it faster.

Good luck to you!