note 32081 modified in function.explode by aidan

From: Date: Sun, 27 Jun 2004 14:17:26 +0000
Subject: note 32081 modified in function.explode by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71922@lists.php.net to get a copy of this message
I had problems with the above function returning empties when there shouldn't be. Here's my take. I hope some of you find this of value. <?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]; } } return $out; } ?> --was-- I had problems with the above function returning empties when there shouldn't be. Here's my take. I hope some of you find this of value. 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]; } } return $out; } http://php.net/manual/en/function.explode.php

« previous php.notes (#71922) next »