note 32247 modified in function.explode by aidan

From: Date: Sun, 27 Jun 2004 14:15:21 +0000
Subject: note 32247 modified in function.explode by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71916@lists.php.net to get a copy of this message
Your funcion ignores the last element of the CSV string. You have to add the line "$out[]=$word;" just befor the "return $out;". The function now looks like: <?php function csv_explode2($str, $delim=',', $qual="\"") { $len=strlen($str); $inside=0; for ($i =0; $i<$len; $i++) { if ($str[$i]==$delim && !$inside) { $out[]=$word; $word=''; } else if ($str[$i]==$qual) { $inside=!$inside; } else { $word.=$str[$i]; } } $out[]=$word; return $out; } ?> --was-- Your funcion ignores the last element of the CSV string. You have to add the line "$out[]=$word;" just befor the "return $out;". The function now looks like: function csv_explode2($str, $delim=',', $qual="\"") { $len=strlen($str); $inside=0; for ($i =0; $i<$len; $i++) { if ($str[$i]==$delim && !$inside) { $out[]=$word; $word=''; } else if ($str[$i]==$qual) { $inside=!$inside; } else { $word.=$str[$i]; } } $out[]=$word; return $out; } http://php.net/manual/en/function.explode.php

« previous php.notes (#71916) next »