note 84783 added to function.fputcsv

From: Date: Wed, 30 Jul 2008 05:44:47 +0000
Subject: note 84783 added to function.fputcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-142915@lists.php.net to get a copy of this message
I found that the fputcsv examples for PHP 4 missed one thing, that was proper handling of the $enclosure value when it is a quote (if a quote is passed in a field, and it is delimited by a slash, it will be improperly handled by the functions submitted here). My modified function was built using the actual PHP5 source for fputcsv, with the addition of properly reacting to the existence of a delimited quote in the field being processed. <?php if (!function_exists('fputcsv')) { function fputcsv(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"') { // Sanity Check if (!is_resource($handle)) { trigger_error('fputcsv() expects parameter 1 to be resource, ' . gettype($handle) . ' given', E_USER_WARNING); return false; } if ($delimiter!=NULL) { if( strlen($delimiter) < 1 ) { trigger_error('delimiter must be a character', E_USER_WARNING); return false; }elseif( strlen($delimiter) > 1 ) { trigger_error('delimiter must be a single character', E_USER_NOTICE); } /* use first character from string */ $delimiter = $delimiter[0]; } if( $enclosure!=NULL ) { if( strlen($enclosure) < 1 ) { trigger_error('enclosure must be a character', E_USER_WARNING); return false; }elseif( strlen($enclosure) > 1 ) { trigger_error('enclosure must be a single character', E_USER_NOTICE); } /* use first character from string */ $enclosure = $enclosure[0]; } $i = 0; $csvline = ''; $escape_char = '\\'; $field_cnt = count($fields); $enc_is_quote = in_array($enclosure, array('"',"'")); reset($fields); foreach( $fields AS $field ) { /* enclose a field that contains a delimiter, an enclosure character, or a newline */ if( is_string($field) && ( strpos($field, $delimiter)!==false || strpos($field, $enclosure)!==false || strpos($field, $escape_char)!==false || strpos($field, "\n")!==false || strpos($field, "\r")!==false || strpos($field, "\t")!==false || strpos($field, ' ')!==false ) ) { $field_len = strlen($field); $escaped = 0; $csvline .= $enclosure; for( $ch = 0; $ch < $field_len; $ch++ ) { if( $field[$ch] == $escape_char && $field[$ch+1] == $enclosure && $enc_is_quote ) { continue; }elseif( $field[$ch] == $escape_char ) { $escaped = 1; }elseif( !$escaped && $field[$ch] == $enclosure ) { $csvline .= $enclosure; }else{ $escaped = 0; } $csvline .= $field[$ch]; } $csvline .= $enclosure; } else { $csvline .= $field; } if( $i++ != $field_cnt ) { $csvline .= $delimiter; } } $csvline .= "\n"; return fwrite($handle, $csvline); } } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 24.40.216.236 ---- Manual Page -- http://www.php.net/manual/en/function.fputcsv.php Edit -- https://master.php.net/note/edit/84783 Del: integrated -- https://master.php.net/note/delete/84783/integrated Del: useless -- https://master.php.net/note/delete/84783/useless Del: bad code -- https://master.php.net/note/delete/84783/bad+code Del: spam -- https://master.php.net/note/delete/84783/spam Del: non-english -- https://master.php.net/note/delete/84783/non-english Del: in docs -- https://master.php.net/note/delete/84783/in+docs Del: other reasons-- https://master.php.net/note/delete/84783 Reject -- https://master.php.net/note/reject/84783 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#142915) next »