escaping commas for csv output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce

    escaping commas for csv output

    I'm outputting form content into a csv file. If a comma is used in one
    of the fields, however, it will interpret to go to next column. Is there
    a workaround? Thanks.

    $fp = fopen('my.csv', 'a');
    $content = "$var1,$var2,$v ar3...
    fwrite($fp,$con tent);
  • Michael Fesser

    #2
    Re: escaping commas for csv output

    ..oO(Bruce)
    >I'm outputting form content into a csv file. If a comma is used in one
    >of the fields, however, it will interpret to go to next column. Is there
    >a workaround? Thanks.
    >
    >$fp = fopen('my.csv', 'a');
    >$content = "$var1,$var2,$v ar3...
    >fwrite($fp,$co ntent);
    fputcsv()

    Micha

    Comment

    • Sjoerd

      #3
      Re: escaping commas for csv output

      Bruce wrote:
      I'm outputting form content into a csv file. If a comma is used in one
      of the fields, however, it will interpret to go to next column.
      You should use fputcsv().

      Alternatively, there are two typical solutions for this:
      1) use another delimiter which is less likely to occur in the data
      2) escape the delimiter

      Of course, you need both.

      Solution 1: Consider for example that every var is quoted like this:
      "banana", "apple", "pear"
      Now, the delimiter has become ", ", and there is a " at the beginning and
      the end. However, if the string ", " occurs in the data, you still have a
      problem.

      Solution 2: Consider that a comma may occur in the data. You have to
      distinguish between a comma in the data and a comma at the end of the
      data. This is done by preceding the comma with an escape character:
      banana, apple, pear, hello\, I said
      Here, the comma in the data is preceded by a backslash, to indicate that
      it belongs to the data. But now, if you want to put \, in the data, it
      will remove the backslash. Therefore, you also escape the backslash, like
      so: \\,

      A backslash is a typical escape character, but fputcsv() uses quotes to
      escape quotes, so "" becomes ":
      "banana", "apple", """Hello"", I said"

      Comment

      • Bruce

        #4
        Re: escaping commas for csv output

        Michael Fesser wrote:
        >>
        >$fp = fopen('my.csv', 'a');
        >$content = "$var1,$var2,$v ar3...
        >fwrite($fp,$co ntent);
        >
        fputcsv()
        >
        Micha
        I'm assuming replacing fwrite() with fputcsv().

        Was there something else (it did not work)?

        Comment

        • Michael Fesser

          #5
          Re: escaping commas for csv output

          ..oO(Bruce)
          >Michael Fesser wrote:
          >>>
          >>$fp = fopen('my.csv', 'a');
          >>$content = "$var1,$var2,$v ar3...
          >>fwrite($fp,$c ontent);
          >>
          >fputcsv()
          >>
          >Micha
          >
          >I'm assuming replacing fwrite() with fputcsv().
          Not exactly.
          >Was there something else (it did not work)?
          Sometimes it helps to have a look at the manual. There's even an
          example.

          Micha

          Comment

          • Gordon

            #6
            Re: escaping commas for csv output

            On Sep 24, 7:46 pm, Bruce <n...@none.netw rote:
            I'm outputting form content into a csv file. If a comma is used in one
            of the fields, however, it will interpret to go to next column. Is there
            a workaround? Thanks.
            >
            $fp = fopen('my.csv', 'a');
            $content = "$var1,$var2,$v ar3...
            fwrite($fp,$con tent);
            If your aim is to write out data that would be compatible with
            spreadsheet software, then the standard solution is to enclose any
            field that's a string in quotes. For example, if your data was:

            1,2,3,a,b,c,a man, a plan, a canal, panama

            then rendering it as follows should produce the correct results when
            you import into your CSV supporting spreadsheet application:

            "1","2","3","a" ,"b","c","a man, a plan, a canal, panama"

            Of course, you then hit the problem of what to do if your string
            contains a " character?

            1,2,3,a,b,c,a man, a plan, a canal, "panama"

            The answer is that you can escape the " character by doubling it up in
            the output.

            "1","2","3","a" ,"b","c","a man, a plan, a canal, ""panama""

            Data formatted like this should be imported correctly into most
            software that supports with CSV. Do bear in mind, that Microsoft
            Excel can have problems if any of your data contains a newline (the
            general consensus is that if the newline is contained within quotes it
            should be treated as part of the field instead of a line terminator,
            but Excel seems to ignore that).


            Comment

            • Captain Paralytic

              #7
              Re: escaping commas for csv output

              On 24 Sep, 19:46, Bruce <n...@none.netw rote:
              I'm outputting form content into a csv file. If a comma is used in one
              of the fields, however, it will interpret to go to next column. Is there
              a workaround? Thanks.
              >
              $fp = fopen('my.csv', 'a');
              $content = "$var1,$var2,$v ar3...
              fwrite($fp,$con tent);
              If you're planning on using the output for Excel, I would recommend
              using tabs instead of commas to delimit the file.

              Comment

              • Jeff

                #8
                Re: escaping commas for csv output

                Michael Fesser wrote:
                .oO(Bruce)
                >
                >Michael Fesser wrote:
                >>>$fp = fopen('my.csv', 'a');
                >>>$content = "$var1,$var2,$v ar3...
                >>>fwrite($fp,$ content);
                >>fputcsv()
                >>>
                >>Micha
                >I'm assuming replacing fwrite() with fputcsv().
                >
                Not exactly.
                >
                >Was there something else (it did not work)?
                >
                Sometimes it helps to have a look at the manual. There's even an
                example.
                Instead of writing to a file what if we just wanted to "write" to a
                variable, and later write a header and echo that variable. Is there a
                php method of doing that (I couldn't find it). Or should I just roll my
                own, I was going to do that (doesn't seem like much) until I read this
                thread...

                Jeff
                >
                Micha

                Comment

                • Captain Paralytic

                  #9
                  Re: escaping commas for csv output

                  On 27 Sep, 14:10, Jeff <jeff@spam_me_n ot.comwrote:
                  Michael Fesser wrote:
                  .oO(Bruce)
                  >
                  Michael Fesser wrote:
                  >>$fp = fopen('my.csv', 'a');
                  >>$content = "$var1,$var2,$v ar3...
                  >>fwrite($fp,$c ontent);
                  >fputcsv()
                  >
                  >Micha
                  I'm assuming replacing fwrite() with fputcsv().
                  >
                  Not exactly.
                  >
                  Was there something else (it did not work)?
                  >
                  Sometimes it helps to have a look at the manual. There's even an
                  example.
                  >
                  Instead of writing to a file what if we just wanted to "write" to a
                  variable, and later write a header and echo that variable. Is there a
                  php method of doing that (I couldn't find it).
                  There are many examples of this!

                  Comment

                  • Jeff

                    #10
                    Re: escaping commas for csv output

                    Captain Paralytic wrote:
                    On 27 Sep, 14:10, Jeff <jeff@spam_me_n ot.comwrote:
                    >Michael Fesser wrote:
                    >>.oO(Bruce)
                    >>>Michael Fesser wrote:
                    >>>>>$fp = fopen('my.csv', 'a');
                    >>>>>$content = "$var1,$var2,$v ar3...
                    >>>>>fwrite($fp ,$content);
                    >>>>fputcsv()
                    >>>>Micha
                    >>>I'm assuming replacing fwrite() with fputcsv().
                    >>Not exactly.
                    >>>Was there something else (it did not work)?
                    >>Sometimes it helps to have a look at the manual. There's even an
                    >>example.
                    >Instead of writing to a file what if we just wanted to "write" to a
                    >variable, and later write a header and echo that variable. Is there a
                    >php method of doing that (I couldn't find it).
                    There are many examples of this!
                    Well, I found this:


                    $csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');

                    fputcsv($csv, array('blah','b lah'));

                    rewind($csv);

                    // put it all in a variable
                    $output = stream_get_cont ents($csv);

                    But frankly, I don't understand why there isn't a more direct method.

                    function csvLine($array) {
                    $line = array();

                    foreach($array as $item){
                    $item = str_replace('"' ,'""',$item);
                    $line[] = '"' . $item . '"';
                    }

                    $content = implode(',',$li ne);

                    return $content . "\n";
                    }

                    I don't believe Excell would have any trouble opening that.

                    Have I missed something?

                    Jeff

                    Comment

                    Working...