i have asked this one before.

fgetcsv() read data from csv files. it must be a reverse function to write the data to the csv files.

i asked this question. i was told no, there is not a reverse to fgetcsv(). but it makes great sense to have one.

is there anyone writting one of this kind function reverse to fgetcsv() by themeselves?

it seems not so difficult. reading data by column, separate them by comma, test if the value has comma in it, the value will be in quote quote, and the double quote " in the value will be escaped by double double quote "" ...

but i would like to leave this work to the more experienced php programmer.

any help?

Thanks!

    column 1: hello
    column 2: hello, world! "good morning", USA.

    will be transferred to csv as

    hello,"hello, world! ""good morning"", USA."

      Where are the columns coming from? You have them in separate variables? Don't understand you about where they're coming from, but assuming you've got the columns it's a pretty trivial task:

      if(strpos($variable,',')!==false) // if the value has comma in it
          $variable='"'.  // the value will be in double quote
      	str_replace('"','""',$variable). //double quote " in the
      	                                 // value will be escaped by double double quote ""
      	'"';
      

      Does it for one $variable. That's basically just what you said, only in PHP instead of English.

        the column is like in excel, or other database etc.

        there must be a reverse function to fgetcsv(), it is easy to do and definately needed by many people.

        do i really to have write this myself? i have the basic ideas as you suggest.

        when i write this, i have to take care the values with comma but quoted them in the double quotes " ".

        therefore, i also need to escape the double quotes, by using "" for " in the value to avoid the confustion.

        that is all I have to take care? if that is so. why this simple function is not in anywhere?

        someone must already have written this.

        thanks!

          Write a Reply...