note 102748 added to regexp.reference.recursive

From: Date: Thu, 03 Mar 2011 12:49:59 +0000
Subject: note 102748 added to regexp.reference.recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-177526@lists.php.net to get a copy of this message
Here's an approach to create a multidimensional array according to a string's delimiters, i.e. we want to analyze... "some text (aaa(b(c1)(c2)d)e)(test) more text" ... as multidimensional layers. <?php $string = "some text (aaa(b(c1)(c2)d)e)(test) more text"; /* * Analyses the string multidimensionally by its opening and closing delimiters */ function recursiveSplit($string, $layer) { preg_match_all("/\((([^()]*|(?R))*)\)/",$string,$matches); // iterate thru matches and continue recursive split if (count($matches) > 1) { for ($i = 0; $i < count($matches[1]); $i++) { if (is_string($matches[1][$i])) { if (strlen($matches[1][$i]) > 0) { echo "<pre>Layer ".$layer.": ".$matches[1][$i]."</pre><br />"; recursiveSplit($matches[1][$i], $layer + 1); } } } } } recursiveSplit($string, 0); /* Output: Layer 0: aaa(b(c1)(c2)d)e Layer 1: b(c1)(c2)d Layer 2: c1 Layer 2: c2 Layer 0: test */ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 80.219.172.48 ---- Manual Page -- http://www.php.net/manual/en/regexp.reference.recursive.php Edit -- https://master.php.net/note/edit/102748 Del: integrated -- https://master.php.net/note/delete/102748/integrated Del: useless -- https://master.php.net/note/delete/102748/useless Del: bad code -- https://master.php.net/note/delete/102748/bad+code Del: spam -- https://master.php.net/note/delete/102748/spam Del: non-english -- https://master.php.net/note/delete/102748/non-english Del: in docs -- https://master.php.net/note/delete/102748/in+docs Del: other reasons-- https://master.php.net/note/delete/102748 Reject -- https://master.php.net/note/reject/102748 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177526) next »