<- back to archive

September 17
fputcsv in PHP4
I know no-one who actually reads this blog will care, however I needed some place to put this info so it might help someone else. Over the weekend I wrote a replacement function for fputcsv that would work inside PHP 4. It is pretty much a straight copy from the PHP source code written in C.
<?php

if (!function_exists('fputcsv')) {
  
  function fputcsv(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"') {
    $str = '';
    $escape_char = '\\';
    foreach ($fields as $value) {
      settype($value, 'string');
      if (strpos($value, $delimiter) !== false ||
          strpos($value, $enclosure) !== false ||
          strpos($value, "\n") !== false ||
          strpos($value, "\r") !== false ||
          strpos($value, "  ") !== false ||
          strpos($value, ' ') !== false) {
        $str2 = $enclosure;
        $escaped = 0;
        $len = strlen($value);
        for ($i=0;$i<$len;$i++) {
          if ($value[$i] == $escape_char) {
            $escaped = 1;
          } else if (!$escaped && $value[$i] == $enclosure) {
            $str2 .= $enclosure;
          } else {
            $escaped = 0;
          }
          $str2 .= $value[$i];
        }
        $str2 .= $enclosure;
        $str .= $str2.$delimiter;
      } else {
        $str .= $value.$delimiter;
      }
    }
    $str = substr($str,0,-1);
    $str .= "\n";
    return fwrite($handle, $str);
  }
  
}

?>
comments
Good for you!!!
I'll smile and nod and pretend i know what all of that gobble-de-gook means!!! :)
- Andrea
Thank you. Thank you. Thank you.

Now *I* don't have to spend my whole weekend writing a replacement for fputcsv. How weird that php4 has the read but not the write for this (?!$)

Again thanks, you made my weekend way better.
- canton
Many thanks for this... so annoying when you see a function for php5, that you can't use because your on 4.... so this was a big help

(although we should be on PHP5 in the next couple of months)
- james dunmroe
Gracias! estaba buscando este código y creo que solo vos lo tenes... Excelente!
- David
hi adam and nee and my little elli and of course brooke . tried to get to my email but must have wrong pass word so i will write to you here till i figure it out . just left las vegas what a place the lights down the strip are great to look at but that place is mad so many people . the grand canon was beauitful but the day we were there was a lot of smoke so couldn't see very far . tonight we are fresno nice town about as big as the gold coast ,travling today to national park to see brown bears at yosmitiy .we did buy a mobil phone but we are still trying to work out how to call a mobil or text will ring you at home on week-end. yes dad has checked out a lot of loos . hawiiai was the best so far i'm going to go there again . tshirts at the market 8 for 20 bucks , revlon eye liner i pay 30 bucks for at myer 5 bucks at the supermarket . food is pretty cheap but man the size of them is ridoulas dave and i can share one . they done sell many vegies here so i am missing things like sweet potato ,pumpkin ,peas and there carrots are yellow . but been havig lots of soup of and salads . dennys is a big resturant chain here food is pretty good and fresh . any way better go have some breaky will ring soon love you all and miss you heaps and heaps mum and dad xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxooxoxoxoxxoxoxoxoxoxoooox
ps can you belive that agirl who works with joy , stews wife is on the same trip. love you all mum
- tammy
I love the fact that the only comment on this blog in ages (since Nee fest basically) has been an email from Tammy and Dave :P

Also love the fact that it was put under the fputcsv post.
- Ed
post a comment