Towards the end of November, I shared a quick tip on how to extract a certain type of data with RegEx. As mentioned in the post, this is all part of the process of working to create a certain type of CSV.

And for anyone else who has worked on a similar process, the odds are pretty high that you follow a similar process each time to work on creating a CSV generator.

Creating a CSV in WordPress

For example, the process generally entails:

  1. Creating an empty file to write the data
  2. Query the database for the records
  3. Iterate through the records adding characters where necessary
  4. Concatenating the record into a string
  5. Writing the string to a file

Or something similar to this. The thing is, there are likely minor idiosyncrasies that come with
creating a CSV generator.

For example, let’s say that each comma-delimited value needs to be offset by quotes, or let’s say that at the end of each line you’re appending a comma where there really shouldn’t be one, or say that there’s multiple values separated by another character (like perhaps a ‘|’ character) in each comma delimited value.

And Extra Characters?

Whatever the case, this means that there is still a level of processing that you’ll need to do on each line before it’s ready to be written to file.

For example, let’s say that you’re iterating through a record and you’re building up a line for the string to be written to file that looks like this:

Note that this is but one way to do it. This is not the way to do it, so YMMV when it comes to actually implementing a generator like this.

Anyway, this is all well and good but you’re left with one extra comma (and thus one extra column if you were to open this in spreadsheet software).

To that end, you’ll need to remove the final character from the string before adding it to the data to be written to file:

Again, this isn’t a definitive way to do it. After all, the way in which you go about creating your CSV may be a bit different, but the general idea still stands: Make sure that you’re not leaving extraneous characters at the end of each line for the sake of creating empty columns that aren’t necessary.


Join the conversation! 4 Comments

  1. I just had to deal with this on a project recently. Lots of issues to consider with CSVs, so I used this library Was really easy to use and saved me a ton of time trying to do it from scratch.

  2. PHP has some built in helpers like fputcsv
    for this kind of stuff.
    Did you consider using that?

    • Yes, definitely, but there are some nuances with some CSVs that need more than the standard “Value 1”, “Value 2”, …, “Value N” such that you need to, say, place multiple values in one column, for example.

Leave a Reply