note 71278 added to function.explode

From: Date: Mon, 20 Nov 2006 22:08:09 +0000
Subject: note 71278 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-119911@lists.php.net to get a copy of this message
I needed an explode funciton that would ignore commas inside a set of double quotes, in which I found that PHP ignores. So I wrote this litle fucntion that would do just that. I know that EXCEL places strings into " " fields, and numbers without, which really screwed things up. I hope this helps for those of you out there that may have actually thought that your CSV files worked, and may not actually work... until you find it too late! <?php function explodeWithString($delimiter, $line) { $line = trim($line); $lineLen = strlen($line); $arrayCntr = 0; $string = false; for($i = 0; $i < $lineLen; $i++) { if($line{$i} == '"' && $string == false) { $string = true; continue; } elseif($line{$i} == '"' && $string == true) { $string = false; continue; } elseif($line{$i} == $delimiter && $string == false) { $arrayCntr++; continue; } $array[$arrayCntr] .= $line{$i}; } return $array; } ?> ---- Server IP: 66.207.199.35 Probable Submitter: 207.54.112.109 ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/71278 Del: integrated -- https://master.php.net/note/delete/71278/integrated Del: useless -- https://master.php.net/note/delete/71278/useless Del: bad code -- https://master.php.net/note/delete/71278/bad+code Del: spam -- https://master.php.net/note/delete/71278/spam Del: non-english -- https://master.php.net/note/delete/71278/non-english Del: in docs -- https://master.php.net/note/delete/71278/in+docs Del: other reasons-- https://master.php.net/note/delete/71278 Reject -- https://master.php.net/note/reject/71278 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119911) next »