Ralisten
03-05-2005, 11:20 AM
Is there any easy way to escape commas in PHP for output into a CSV file other than looking for each comma and replacing it with '\,'? addslashes() and even mysql_escape_string() seems to ignore commas.
![]() | View Full Version : Escaping commas for a CSV file Ralisten 03-05-2005, 11:20 AM Is there any easy way to escape commas in PHP for output into a CSV file other than looking for each comma and replacing it with '\,'? addslashes() and even mysql_escape_string() seems to ignore commas. folsom 03-05-2005, 12:25 PM Every program that imports csv files works different. but I was targeting excel 97 when I worked through this so ymmv. If you need a comma in a text field, then you can quote the whole text field. "abc,def" is one field. if you need a quote in a text field, then you can use a double quote "". For example "bob said ""foo"""" would put one quote on each side of foo. Burhan 03-06-2005, 02:21 AM If you don't need a comma, you can replace it with any character, it just needs to be the same. I know Excel allows you to choose what is the separator when importing files: foo,bar|zoo|jim,bob|abracadabra Ralisten 03-06-2005, 01:00 PM I went ahead and used an alternate character just because in testing, everyone used commas in one of the textboxes, which really screws up CSV files :) Thanks! rodloos 03-06-2005, 04:23 PM TSV (Tab Separated Values) is a fairly standard alternative to CSV. That is what we use for creating files to be opened in Excel. Set the Mime-Type for Excel, and the downloaded file gets automatically opened in Excel with no problems. Burhan 03-07-2005, 02:45 AM Actually, you can choose any character for the limiter, just change the extension of the file to .csv and Excel will open it. It should then ask you what is the field limiter, etc. |