Part of my application is going to allow users to copy and paste data either as comma delimited or tabbed delimited.
str_getcsv should hopefully handle this nicely, but can anyone advise on the best way to pre-determine what delimiter I should use?
My initial thought was to explode the data using a delimiter char and then check the size of the array, but then if my string is tab delimited and I use explode with a comma, if part of my data contains a comma, I’m going to get false results?
I guess I could do an explode using both characters and then assume the biggest array is what is used as the delimiter?
Most spreadsheet applications assume the delimiter is a comma, and provide an interface for users to specify alternatives.
You could experiment with looking for escaped characters. If a character has been escaped, there’s a pretty good chance it’s a delimiter.
If you find a line that starts with a quote, you can jump to the next un-escaped quote and you should find a delimiter there.
This might be a silly question but if there is going to be variety in the separator characters, will any given input be assured to be well-formed? Also, where are the users copy-and-pasting from, and do you have control over that?