note 75876 deleted from function.explode by tularis
| From: | tularis@php.net | Date: | Mon, 27 Oct 2008 13:05:29 +0000 |
| Subject: | note 75876 deleted from function.explode by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145818@lists.php.net to get a copy of this message | ||
Note Submitter: tajhlande at gmail dot com
----
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;
}
?>