tags:

views:

83

answers:

2

I have a PHP program that uploads a CSV file and reads through it.
We only have PC's in the office, but sometimes our clients have Macs. Is there a way to create a Mac CSV in Windows for testing purposes?

A: 

There's no such thing as a 'Mac CSV'. A CSV is a document with values that are comma-separated (that's why they call it a "Coma-Separated Values file). In other words, it's plain text.

The only edge cases I could see are line endings (which is likely to be \n but could also be \r) and encoding (which is very likely to be UTF-8 but could somehow be Mac Roman) if your clients use non-ASCII characters.

zneak
Ok, so if it's not a 'Mac CSV', is there a way to make a CSV with the \r line endings in windows?
Brian
In PHP, load the file and use str_replace to replace all occurences of "\n" by "\r". But since this is so simple, depending on how complex is your CSV system, you might as well just check that it treats the case correctly.
zneak
+1  A: 

Since CSV is a text-based format, the only difference between a Mac and a PC CSV file would be the line endings. Windows typically use CR+LF for line endings, whereas Mac OS X and *nix typically use just LF.

It really depends on the software that will be reading the CSV file. There is a good chance that a well-designed reader (on either Mac or PC) would gracefully / automatically handle either LF or CR+LF endings. However, there is (naturally) no shortage of not-so-well-written software out there. Do you have more info on what software they will be using?

If you want to explicitly create a CSV file in "Mac-friendly" mode, you could open the file in binary mode, and explicitly use "\n" (rather than "\r\n") at the end of each line. This will likely open / view fine on most apps within windows. (Though Notepad won't like it, Excel probably will.) This file should then be "optimized" for Mac output. Though, as above, it might not matter at all, if the client's software is "tolerant" of different endings.

EDIT: Prior to OS X, Mac used CR (\r) for line endings. For OS X, they switched to LF (\n). Most likely, your clients are using OS X, so \n is likely the ending you want.

Eric Pi
Aaah, I see that I've misread part of the original question-- you want to simulate a CSV file uploaded in Mac format, rather than to create a Mac-compatible file. In that case, my suggestion should still stand-- Open the file in "binary" mode (using the "b" flag to fopen(), and use "\n" as the line endings.
Eric Pi
Is there a way I can alter the file in windows? It's a very small CSV only about 10 lines
Brian
To alter an existing file in Windows, you would need to use a binary editor. I'm sure you can find many free ones with a search. Once you have the file open, just remove the CR characters (0x0d)... You should be left with a Mac formatted file.
Eric Pi