note 29775 deleted from function.explode by bjori

From: Date: Mon, 25 Sep 2006 08:14:15 +0000
Subject: note 29775 deleted from function.explode by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117612@lists.php.net to get a copy of this message
Note Submitter: tengel at fluid dot com ---- Here's a little function similar to the above which will traverse a string, but pay attention to quotes inline. I.e., I needed to parse something like this into an array: one two "third thing" "fourth thing" five It's pretty slow, but it works. <?php function opt_explode($echar, $str) { if (strlen($echar) != 1 || strlen($str) == 0) { return 0; } $str = trim($str); // input string index counter $idx=0; // output array element counter $arr=0; while($idx < strlen($str)) { if($str[$idx] == '"') { // quoted field $idx++; while ($idx < strlen($str)) { // look for ending quote if($str[$idx] == '"') { $idx++; $arr++; break; } $newstrarr[$arr] = $newstrarr[$arr] . $str[$idx]; $idx++; } } elseif ($str[$idx] == $echar) { // normal delimiter, advance element $arr++; } else { // must be normal char, tack onto current element $newstrarr[$arr] = $newstrarr[$arr] . $str[$idx]; } $idx++; } return $newstrarr; } ?>

« previous php.notes (#117612) next »