note 96942 deleted from function.explode by danbrown

From: Date: Wed, 24 Mar 2010 15:41:09 +0000
Subject: note 96942 deleted from function.explode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167352@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- <?php // {{{ public static function explodeRecursive($delims, $string, $delimdelim = NULL) // {{{ Doc /** * Recursively explode a string * * Explodes a string into a multi-demensional array at the delimeters * specified in $delims. The top level is created from the first delim * with subsequent levels at each further delim * * <code> * * $testString = "two bad sentences. In this paragraph</p> * <p>And three. bad sentences in. this paragraph"; * * // To split into paragraphs sentences and words we delimit at </p><p>, * // full stop (.) and space ( ) $delim = '</p><p>,., ' * // setting $delimdelim to comma (,) * $testArray = explodeRecursive('</p><p>,., ', $testString, ','); * * print_r ($testArray); * * // Will output * * Array * ( * [0] => Array * ( * [0] => Array * ( * [0] => two * [1] => bad * [2] => sentences * ) * * [1] => Array * ( * [0] => In * [1] => this * [2] => paragraph * ) * * ) * * [1] => Array * ( * [0] => Array * ( * [0] => And * [1] => three * ) * * [1] => Array * ( * [0] => bad * [1] => sentences * [2] => in * ) * * [2] => Array * ( * [0] => this * [1] => paragraph * ) * * ) * * ) * * </code> * * @param string $string The string to be exploded * * @param string $delims a list of delimiters Each delimiter is one * character unless you specify * a $delimdelim * * @param string $delimdelim Optional single character that specifies the * delimiter in the $delim string. * * @return array * * @author Will Bickerstaff **/ // }}} public static function explodeRecursive($delims, $string, $delimdelim = NULL){ $return = array(); if (strlen($delims) > 1){ if ($delimdelim === NULL){ $delims = str_split($delims); } else { $delims = explode($delimdelim, $delims); } $thisdelim = $delims[0]; array_shift($delims); } else { $thisdelim = $delims; } $return = explode($thisdelim, $string); if (is_array($delims)){ $delims = implode($delimdelim,$delims); foreach($return as $k => $v){ $v = trim($v); $return[$k] = explodeRecursive($delims, $v, $delimdelim); } } return $return; } // }}} ?>

« previous php.notes (#167352) next »