PHP to CSV line break issue

I use the following code to get data from a form and save it as csv.

$cvsData = $name . "," . $address . "\n"; $fp = fopen("file.csv", "a"); if ($fp) { fwrite($fp, $cvsData); // Write information to the file fclose($fp); // Close the file }

When someone enters a comma or line break in address field it breaks the formatting. So how can i escape it so that the whole address stays in the same field ?

-------------Problems Reply------------

Put each data item inside quotation marks. A pair of quotation marks inside a quoted value signifies a single quotation mark. e.g.

"Daniel Norton","Congress Ave.
Austin (""Keeping it weird""), TX"

Referring to your example:

$data = str_replace('"','""',$data);
$address = str_replace('"','""',$address);
$cvsData = "\"$data\",\"$address\"\n";

Better still, just use the PHP function fputcsv.

fputcsv($fp,array($data,$address));

CSV is totally dependent on the software used to read it.

Have a look at http://www.csvreader.com/csv_format.php for some details on how certain programs expect CSV data.

I have created a CSV class that can handle most of these situations. You might want to have a look at it.

https://gist.github.com/1786683

And to answer your question, using that class you could

$csv = CSV::newExcel();
$cvsData = $csv->row($name, $address);
// etc...

Category:php Views:1 Time:2012-02-10

Related post

  • CSV line break issue 2012-11-07

    I have this CSV file that contain this following, 10000,hello 10000,bye 10000,good And, my code below for reading CSV file, and send them to database. if ($_FILES[csv][size] > 0) { //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen

  • Line break issues with 'time' and 'make' 2011-03-23

    I am developing some software in C. One of the targets in my makefile runs a test, and I use time to record the runtime and valgrind to check for memory leaks. When I invoke one of the smaller tests directly from the command line, I get this. $ time

  • Line break issue from CSV to MySQL 2011-01-13

    I am importing a .csv file into MySQL and everything works fine, except the line breaks that are in the file. One of my .csv rows looks like this: 42,E-A-R™ Classic™ Earplugs,ear,images/ear/classic.jpg,5%,"Proven size, shape, and foam 3M's most popul

  • Write out a text file on Linux to be imported by Windows application, line break issues 2011-08-01

    I've read a lot of stuff both on stackoverflow and around the net, I've also tried a dozen variations in my code & am yet to find a solution. The method has to write out a text file according to these file specs I was given, so that another appli

  • dijit.InlineEditBox line-breaks issue in Chrome 2011-08-02

    I am trying to use "dijit.InlineEditBox" on a page to allow user to change a field on the page. The issue I am facing here is it's behavior in Chrome and Firefox is different. I am using the following code. new dijit.InlineEditBox ({ editor: "dijit.f

  • Reportviewer and RDLC line break issue with records 2012-04-25

    Wonder if anyone can help. I have a c# web application that uses the Reportviewer control that uses RDLC files to create the reports. The problem that I'm having is that any fields in the database that have a line break in them look like this on the

  • hw to create line breaks between dynamically generated labels in a placeholder? 2009-05-06

    This is the code below in code behind file's Page_Load event: LinkButton linkButton = new LinkButton(); linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i; linkButton.ForeColor = Color.Blue; linkButton.Font.Bold = true; linkButton.Font.Size = 14

  • jQuery text() function loses line breaks in IE 2010-12-21

    This is quite a talked about problem on the jQuery forums but I can't figure out a solution for my situation. I have an unordered list with some text elements in them... the user can create new list items via jQuery, which are saved to an SQL databas

  • Thai line breaking: how to break Thai text effectively 2011-12-13

    Situation with Thai text on a client site is that we can't control where exactly particular words/sentences are going to break between the lines (how web browser will handle it). Often, content appearance is indicated as incorrect by local reviewers.

  • Unresolved line wrap issue when sending email from Outlook to hotmail, gmail, yahoo 2012-05-21

    I have just wasted almost two hours of my life searching for a fix, so please disregard (tolerate) my annoyed tone.... All I want to do is send HTML to clients who use hotmail, gmail, and yahoo without having this extremely unprofessional and irritat

  • Line break not happening in CSV file being generated by IE 2009-12-08

    I am using following PHP code to generate CSV file: header("Expires: 0"); header("Cache-control: private"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Description: File Transfer"); header("Content-Type: text/

  • Importing CSV with line breaks in Excel 2007 2010-04-19

    I'm working on a feature to export search results to a CSV file to be opened in Excel. One of the fields is a free-text field, which may contain line breaks, commas, quotations, etc. In order to counteract this, I have wrapped the field in double quo

  • Line breaks in generated csv file driving me crazy 2011-05-10

    I'm trying to make an export of some data i have (stored in a datatable). Some of those values have a linebreak in them. Now every time i try and import the file in excel (2010), the linbreaks get recognised as a new row, instead of an actual linebre

  • reportlab issue with line breaks using XPreformatted - additionall question mark printed 2010-03-10

    I'm using XPreformatted to print some preformated text and I have an issue with the line breaks. The line breaks are correctly translated but additionaly I get a "question mark" at the end of each line. This is my output: first line? second line? som

  • Do the mail() headers have to be terminated with a line break? (IncrediMail issue) 2011-07-12

    I'm using a function for sending UTF8 emails. Since at least one recipient has issues with emails sent by my function (not UTF8, so special chars broken; some mail headers appear in body), I'm wondering if I have to put another line break at the end

  • CSV lines in array(), need to preserve line breaks 2011-07-21

    I have an array in the following format - it's essentially an array of preformatted CSV lines - key 0 and 1 in this example have a CSV value containing a line break. Array ( [0] => 'foo,foo,foo,foo bar,foo,a:1:{i:0;s:4:"blah";}' [1] => 'bar,bar

  • Escaping line break in csv php 2011-10-10

    I have a .csv template I'd wish for people to fill up, save it and upload it. The problem is this, assuming some users would insert hidden line breaks in a row, when using fgetcsv() it would output the row broken by the hidden line breaks. How can I

  • parsing PHP error log issue – line-breakes (\n) inside the erorr messages 2012-01-06

    I am trying to parse PHP error log. The issue is that breaking the file by \n doesn't work. explode(PHP_EOL, $log) This doesn't work because there are some error messages that contain \n by itself. How to break such file by lines then? Extract of the

  • Strange line break character appearing in C# generated CSV 2012-02-11

    I use a OleDb data reader to read a number of records, and then write them to a CSV. I then read from this CSV using File.ReadAllLines, then split on commas to get my data. The problem is some parts of the CSV include a character I can't display (sho

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.200 (s). 11 q(s)