Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>this shows a simple manual parser i wrote with example input with qualifed, non-qualified, escape feature. it can be used for the header and data rows and included an assoc array function to make your data into a kvp style array.</p> <pre><code>//example data $fields = strparser('"first","second","third","fourth","fifth","sixth","seventh"'); print_r(makeAssocArray($fields, strparser('"asdf","bla\"1","bl,ah2","bl,ah\"3",123,34.234,"k;jsdfj ;alsjf;"'))); //do something like this $fields = strparser(&lt;csvfirstline&gt;); foreach ($lines as $line) $data = makeAssocArray($fields, strparser($line)); function strparser($string, $div = ",", $qual = "\"", $esc = "\\") { $buff = ""; $data = array(); $isQual = false; //the result will be a qualifier $inQual = false; //currently parseing inside qualifier //itereate through string each byte for ($i = 0; $i &lt; strlen($string); $i++) { switch ($string[$i]) { case $esc: //add next byte to buffer and skip it $buff .= $string[$i+1]; $i++; break; case $qual: //see if this is escaped qualifier if (!$inQual) { $isQual = true; $inQual = true; break; } else { $inQual = false; //done parseing qualifier break; } case $div: if (!$inQual) { $data[] = $buff; //add value to data $buff = ""; //reset buffer break; } default: $buff .= $string[$i]; } } //get last item as it doesnt have a divider $data[] = $buff; return $data; } function makeAssocArray($fields, $data) { foreach ($fields as $key =&gt; $field) $array[$field] = $data[$key]; return $array; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload