CSV parsing

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

    #1

    CSV parsing

    Hello all,

    Anyone know of a way to call fgetcsv and fputcsv using a string rather than
    a resource? (i.e.., parse a comma delimited string without writing it first
    to a file.)

    Thanks,
    Kevin


  • Ken Robinson

    #2
    Re: CSV parsing


    Kevin Lin wrote (in part):[color=blue]
    > Anyone know of a way to call fgetcsv and fputcsv using a string[/color]
    rather than[color=blue]
    > a resource? (i.e.., parse a comma delimited string without writing[/color]
    it first[color=blue]
    > to a file.)[/color]

    Use the explode() function:

    <?
    $str = explode(',','Th is,is,a,test');
    ?>

    Results in the array $str with four elements:
    $str[0] = 'This'
    $str[1] = 'is'
    $str[2] = 'a'
    $str[3] = 'test'

    Ken

    Comment

    • Kevin Lin

      #3
      Re: CSV parsing

      Thanks for the reply, but explode doesn't handle quoted cases, e.g.:

      'Item1,"Item A,ItemB","Items ""one,"" ""two,"" and ""three"""'

      should get parsed to:

      $array[0]='Item1'
      $array[1]='Item A,Item B'
      $array[2]='Items "one," "two," and "three"'


      "Ken Robinson" <kenrbnsn@rbnsn .com> wrote in message
      news:1101230337 .665372.261980@ c13g2000cwb.goo glegroups.com.. .[color=blue]
      >
      > Kevin Lin wrote (in part):[color=green]
      > > Anyone know of a way to call fgetcsv and fputcsv using a string[/color]
      > rather than[color=green]
      > > a resource? (i.e.., parse a comma delimited string without writing[/color]
      > it first[color=green]
      > > to a file.)[/color]
      >
      > Use the explode() function:
      >
      > <?
      > $str = explode(',','Th is,is,a,test');
      > ?>
      >
      > Results in the array $str with four elements:
      > $str[0] = 'This'
      > $str[1] = 'is'
      > $str[2] = 'a'
      > $str[3] = 'test'
      >
      > Ken
      >[/color]


      Comment

      • Daniel Tryba

        #4
        Re: CSV parsing

        Kevin Lin <kevin@wx3remov e4spam.com> wrote:[color=blue]
        > Thanks for the reply, but explode doesn't handle quoted cases, e.g.:[/color]
        [snip]

        By reading the user comments on the fgetcsv manual page one can find
        http://bu.orbitel.bg/fgetcsvfromline.php. Seems the solution that you
        are looking for.

        Comment

        • Chung Leong

          #5
          Re: CSV parsing


          "Kevin Lin" <kevin@wx3REMOV E4SPAM.com> wrote in message
          news:hdKod.5574 18$mD.522470@at tbi_s02...[color=blue]
          > Hello all,
          >
          > Anyone know of a way to call fgetcsv and fputcsv using a string rather[/color]
          than[color=blue]
          > a resource? (i.e.., parse a comma delimited string without writing it[/color]
          first[color=blue]
          > to a file.)
          >
          > Thanks,
          > Kevin
          >
          >[/color]

          Use the example class at



          Comment

          • R. Rajesh Jeba Anbiah

            #6
            Re: CSV parsing

            "Kevin Lin" <kevin@wx3REMOV E4SPAM.com> wrote in message news:<hdKod.557 418$mD.522470@a ttbi_s02>...[color=blue]
            > Hello all,
            >
            > Anyone know of a way to call fgetcsv and fputcsv using a string rather than
            > a resource? (i.e.., parse a comma delimited string without writing it first
            > to a file.)[/color]

            IMHO, the most dependable way is to use existing fgetcsv along with
            a temporary file:

            <?php
            function ParseCSVString( $str, $delim=',')
            {
            //write the CSV string to a temporary file so that fgetcsv() can be
            used to process...
            $fp = tmpfile();
            fwrite($fp, $str);
            rewind($fp); //rewind to process CSV
            $csv_length = strlen($str);
            $data_arr = fgetcsv($fp, $csv_length, $delim);
            fclose($fp); //clean up temp file
            return $data_arr; //return the array
            }
            ?>

            Or may use csv_explode() found at <http://in.php.net/explode#37004>

            --
            <?php echo 'Just another PHP saint'; ?>
            Email: rrjanbiah-at-Y!com

            Comment

            • Kevin Lin

              #7
              Re: CSV parsing

              This looks useful, thanks.

              "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
              news:bKCdnWRHkO BBdT7cRVn-tg@comcast.com. ..[color=blue]
              >
              > "Kevin Lin" <kevin@wx3REMOV E4SPAM.com> wrote in message
              > news:hdKod.5574 18$mD.522470@at tbi_s02...[color=green]
              > > Hello all,
              > >
              > > Anyone know of a way to call fgetcsv and fputcsv using a string rather[/color]
              > than[color=green]
              > > a resource? (i.e.., parse a comma delimited string without writing it[/color]
              > first[color=green]
              > > to a file.)
              > >
              > > Thanks,
              > > Kevin
              > >
              > >[/color]
              >
              > Use the example class at
              > http://us2.php.net/manual/en/functio...r-register.php
              >
              >[/color]


              Comment

              Working...