note 50113 added to function.split

From: Date: Fri, 18 Feb 2005 11:53:05 +0000
Subject: note 50113 added to function.split
Groups: php.notes 
Request: Send a blank email to php-notes+get-85206@lists.php.net to get a copy of this message
moritz's quotesplit didn't work for me. It seemed to split on a comma even though it was between a pair of quotes. However, this did work: function quotesplit($s, $splitter=',') { //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 = split('\"', $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 result array. $result[] = $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 ) ); 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+50113 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50113&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50113&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50113&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50113&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50113&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50113&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85206) next »