Core Coding®

PHP CSV Parsing

  PHP CSV Parsing
A great way to parse csv files easily.
<?php
function csv_explode($str, $delim = ',', $qual = "\"") {
$len = strlen($str);
$inside = false;
$word = '';
for (
$i = 0; $i < $len; ++$i) {
if (
$str[$i]==$delim && !$inside) {
$out[] = $word;
$word = '';
} else if (
$inside && $str[$i]==$qual && ($i<$len && $str[$i+1]==$qual)) {
$word .= $qual;
++
$i;
} else if (
$str[$i] == $qual) {
$inside = !$inside;
} else {
$word .= $str[$i];
}
}
$out[] = $word;

return
$out;
}


Go back to resources
Home Mail GitHub