note 54304 added to function.split

From: Date: Thu, 30 Jun 2005 03:50:39 +0000
Subject: note 54304 added to function.split
Groups: php.notes 
Request: Send a blank email to php-notes+get-91170@lists.php.net to get a copy of this message
wchris's quotesplit assumes that anything that is quoted must also be a complete delimiter-seperated entry by itself. This version does not. It also uses split's argument order. function quotesplit( $splitter=',', $s ) { //First step is to split it up into the bits that are surrounded by quotes //and the bits that aren't. Adding the delimiter to the ends simplifies //the logic further down $getstrings = explode('"', $splitter.$s.$splitter); //$instring toggles so we know if we are in a quoted string or not $delimlen = strlen($splitter); $instring = 0; while (list($arg, $val) = each($getstrings)) { if ($instring==1) { //Add the whole string, untouched to the previous value in the array $result[count($result)-1] = $result[count($result)-1].$val; $instring = 0; } else { //Break up the string according to the delimiter character //Each string has extraneous delimiters around it (inc the ones we added //above), so they need to be stripped off $temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen+1 ) ); while(list($iarg, $ival) = each($temparray)) { $result[] = trim($ival); } $instring = 1; } } return $result; } ---- Manual Page -- http://www.php.net/manual/en/function.split.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54304 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54304&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54304&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54304&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54304&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54304&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54304&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54304&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91170) next »