<?php $regex = <<<REGEX / #starting pattern delimiter (?> #starting atomic group, intended for performance ( #capture group 1 \[ #literal opening square brace (?: #non-capturing group for logic encapsulation (?> #starting atomic group, intended for performance [^[\]]+ #negate character class to match one or more non-square brace characters ) #closing atomic group | #or (?1) #recurse the subpattern contained within the first capture group )* #close non-capturing group and allow it to be repeated zero or more times \] #literal closing square brace ) #close capture group 1 | #or [^,[\]]+ #negate character class to match one or more non-square brace characters and non-commas )+ #close atomic group and require one or more matches of internal logic \K #forget previously matched characters to prevent losing them during splitting ,? #optionally match a comma /x REGEX; /* (cannot comment after pattern end) x modifier enables inline regex commenting */ var_export( preg_split( $regex, /* '/(?>(\[(?:(?>[^[\]]+)|(?1))*\])|[^,[\]]+)+\K,?/', */ "first,second[,b],third[a,b[1,2,3]],fourth[a[1,2]],sixth", 0, PREG_SPLIT_NO_EMPTY ) );
You have javascript disabled. You will not be able to edit any code.