note 73851 added to function.fgetcsv

From: Date: Tue, 13 Mar 2007 08:09:24 +0000
Subject: note 73851 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-124397@lists.php.net to get a copy of this message
I had a problem with fgetcsv and multibyte characters so i used one of functions below (16-Nov-2002 04:01 to be specific) and modified it to be (hopefully) multibyte safe. <?php /** * @param the csv line to be split * @param the delimiter to split by (default ';' ) * @param if this is false, the quotation marks won't be removed from the fields (default true) */ function mb_csv_split($line, $delim = ';', $removeQuotes = true) { $fields = array(); $fldCount = 0; $inQuotes = false; for ($i = 0; $i < mb_strlen($line); $i++) { if (!isset($fields[$fldCount])) $fields[$fldCount] = ""; $tmp = mb_substr($line, $i, mb_strlen($delim)); if ($tmp === $delim && !$inQuotes) { $fldCount++; $i+= mb_strlen($delim) - 1; } else if ($fields[$fldCount] == "" && mb_substr($line, $i, 1) == '"' && !$inQuotes) { if (!$removeQuotes) $fields[$fldCount] .= mb_substr($line, $i, 1); $inQuotes = true; } else if (mb_substr($line, $i, 1) == '"') { if (mb_substr($line, $i+1, 1) == '"') { $i++; $fields[$fldCount] .= mb_substr($line, $i, 1); } else { if (!$removeQuotes) $fields[$fldCount] .= mb_substr($line, $i, 1); $inQuotes = false; } } else { $fields[$fldCount] .= mb_substr($line, $i, 1); } } return $fields; } ?> ---- Server IP: 147.32.160.17 Probable Submitter: 85.207.75.82 ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/73851 Del: integrated -- https://master.php.net/note/delete/73851/integrated Del: useless -- https://master.php.net/note/delete/73851/useless Del: bad code -- https://master.php.net/note/delete/73851/bad+code Del: spam -- https://master.php.net/note/delete/73851/spam Del: non-english -- https://master.php.net/note/delete/73851/non-english Del: in docs -- https://master.php.net/note/delete/73851/in+docs Del: other reasons-- https://master.php.net/note/delete/73851 Reject -- https://master.php.net/note/reject/73851 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124397) next »