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);
}
}
?>
I'll smile and nod and pretend i know what all of that gobble-de-gook means!!! :)
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.
(although we should be on PHP5 in the next couple of months)
ps can you belive that agirl who works with joy , stews wife is on the same trip. love you all mum
Also love the fact that it was put under the fputcsv post.