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:
- Creating an empty file to write the data
- Query the database for the records
- Iterate through the records adding characters where necessary
- Concatenating the record into a string
- 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.
Leave a Reply
You must be logged in to post a comment.