note 84269 added to function.explode

From: Date: Mon, 07 Jul 2008 05:16:30 +0000
Subject: note 84269 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-141222@lists.php.net to get a copy of this message
<?php /* This function recursively explodes a string at the boundaries of the strings within the array delimiters One possible scenario: You concatenated rows and columns with GROUP_CONCAT() in MySQL and want to access the values in an array in PHP @author Tobias Schultze @date 2008-07-07 */ function explode_recursive($delimiters, $string) { if (!is_array($delimiters)) { $delimiters = array($delimiters); } $delimiter = array_shift($delimiters); if (!empty($delimiters)) { $arExploded = explode($delimiter, $string); if ($arExploded !== false) { foreach ($arExploded as $key => $value) { $arExploded[$key] = explode_recursive($delimiters, $value); } } return $arExploded; } else { return explode($delimiter, $string); } } // Examples: $strSimpleText = 'row 0 column 0--row 0 column 1--row 0 column 2__row 1 column 0--row 1 column 1--row 1 column 2__row 2 column 0--row 2 column 1--row 2 column 2'; $arSimpleDelimiters = array('__','--'); var_dump(explode_recursive($arSimpleDelimiters, $strSimpleText)); $strComplexText = 'item 0.0.0,item 0.0.1,item 0.0.2;item 0.1.0,item 0.1.1;item 0.2.0,item 0.2.1,item 0.2.2,item 0.2.3|item 1.0.0;item 1.1.0|item 2.0.0,item 2.0.1|item 3.0.0;item 3.1.0,item 3.1.1'; $arComplexDelimiters = array('|',';',','); var_dump(explode_recursive($arComplexDelimiters, $strComplexText)); // Output: array(3) { [0]=> array(3) { [0]=> string(14) "row 0 column 0" [1]=> string(14) "row 0 column 1" [2]=> string(14) "row 0 column 2" } [1]=> array(3) { [0]=> string(14) "row 1 column 0" [1]=> string(14) "row 1 column 1" [2]=> string(14) "row 1 column 2" } [2]=> array(3) { [0]=> string(14) "row 2 column 0" [1]=> string(14) "row 2 column 1" [2]=> string(14) "row 2 column 2" } } array(4) { [0]=> array(3) { [0]=> array(3) { [0]=> string(10) "item 0.0.0" [1]=> string(10) "item 0.0.1" [2]=> string(10) "item 0.0.2" } [1]=> array(2) { [0]=> string(10) "item 0.1.0" [1]=> string(10) "item 0.1.1" } [2]=> array(4) { [0]=> string(10) "item 0.2.0" [1]=> string(10) "item 0.2.1" [2]=> string(10) "item 0.2.2" [3]=> string(10) "item 0.2.3" } } [1]=> array(2) { [0]=> array(1) { [0]=> string(10) "item 1.0.0" } [1]=> array(1) { [0]=> string(10) "item 1.1.0" } } [2]=> array(1) { [0]=> array(2) { [0]=> string(10) "item 2.0.0" [1]=> string(10) "item 2.0.1" } } [3]=> array(2) { [0]=> array(1) { [0]=> string(10) "item 3.0.0" } [1]=> array(2) { [0]=> string(10) "item 3.1.0" [1]=> string(10) "item 3.1.1" } } } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 77.185.23.131 ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/84269 Del: integrated -- https://master.php.net/note/delete/84269/integrated Del: useless -- https://master.php.net/note/delete/84269/useless Del: bad code -- https://master.php.net/note/delete/84269/bad+code Del: spam -- https://master.php.net/note/delete/84269/spam Del: non-english -- https://master.php.net/note/delete/84269/non-english Del: in docs -- https://master.php.net/note/delete/84269/in+docs Del: other reasons-- https://master.php.net/note/delete/84269 Reject -- https://master.php.net/note/reject/84269 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141222) next »