Code

Coding, Programming & Algorithms, Tips, Tweaks & Hacks
Search

Sponsored Ads

fputcsv for PHP4

I found it strange why fgetcsv() was included since version 3.0.8 and fputcsv() only since 5.1.0RC1 when both are so closely interrelated. Heres fputcsv for ones not running on a minimum of PHP 5.1.0RC1.
PHP
if (!function_exists('fputcsv'))
 include_once("php4.inc.php");
Assuming php4.inc.php includes functions that natively exist in PHP 5 and not in version 4.
PHP
function fputcsv($fh, $arr)
 {
        $csv = "";
        while (list($key, $val) = each($arr))
         {
                $val = str_replace('"', '""', $val);
                $csv .= '"'.$val.'",';
         }
        $csv = substr($csv, 0, -1);
        $csv .= "\n";
        if (!@fwrite($fh, $csv))
         return FALSE;
 }
The str_replace('"', '""', $val); is because Excel doesn't seem to understand \".
Vanakkam !

1 comments:

wtfbollos said...

why does it output each line 12 times?!?!?!