note 75876 added to function.explode

From: Date: Wed, 20 Jun 2007 00:28:55 +0000
Subject: note 75876 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-127808@lists.php.net to get a copy of this message
While trying to use explode() to parse CSV formatted lines output by MS Excel, I found that if cells contained a comma, then explode() would not behave as desired. So I wrote the following function, which obeys the double quote escaping format output by Excel. Note that it is not sophisticated enough to handle delimiters or escapes that consist of more than one character. I also have no idea how this code will perform when subjected to Unicode data. Use at your own risk. <?php // splits a string into an array of tokens, delimited by delimiter char // tokens in input string containing the delimiter character or the literal escape character are surrounded by a pair of escape characteres // a literal escape character is produced by the escape character appearing twice in sequence // default delimiter character and escape character are suitable for Excel-exported CSV formatted lines function splitWithEscape ($str, $delimiterChar = ',', $escapeChar = '"') { $len = strlen($str); $tokens = array(); $i = 0; $inEscapeSeq = false; $currToken = ''; while ($i < $len) { $c = substr($str, $i, 1); if ($inEscapeSeq) { if ($c == $escapeChar) { // lookahead to see if next character is also an escape char if ($i == ($len - 1)) { // c is last char, so must be end of escape sequence $inEscapeSeq = false; } else if (substr($str, $i + 1, 1) == $escapeChar) { // append literal escape char $currToken .= $escapeChar; $i++; } else { // end of escape sequence $inEscapeSeq = false; } } else { $currToken .= $c; } } else { if ($c == $delimiterChar) { // end of token, flush it array_push($tokens, $currToken); $currToken = ''; } else if ($c == $escapeChar) { // begin escape sequence $inEscapeSeq = true; } else { $currToken .= $c; } } $i++; } // flush the last token array_push($tokens, $currToken); return $tokens; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 66.92.150.171 ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/75876 Del: integrated -- https://master.php.net/note/delete/75876/integrated Del: useless -- https://master.php.net/note/delete/75876/useless Del: bad code -- https://master.php.net/note/delete/75876/bad+code Del: spam -- https://master.php.net/note/delete/75876/spam Del: non-english -- https://master.php.net/note/delete/75876/non-english Del: in docs -- https://master.php.net/note/delete/75876/in+docs Del: other reasons-- https://master.php.net/note/delete/75876 Reject -- https://master.php.net/note/reject/75876 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#127808) next »