note 69450 deleted from function.explode by bjori

From: Date: Mon, 25 Sep 2006 08:05:21 +0000
Subject: note 69450 deleted from function.explode by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117563@lists.php.net to get a copy of this message
Note Submitter: gt at work at hotmail dot com ---- I am sure everyone has their own version for exploding with multiple strings. Here is mine. Please let me know if there are any problems. Thanks. $subject = "This is a test me!ssage: Here is me message yet another finished"; $separators = array(',',' ','!'); $out = array(); class strFunctions { /** * @public * * @brief Explode a string by multiple separators at once * * @param $_subject string Subject to explode * * @param $_separators array Array of strings to separate the subject by * * @param $_out array Array to populate the results with * * @return boolean TRUE if everything was successful, FALSE otherwise */ function explode($_subject, $_separators, &$_out) { if(!is_string($_subject) || !is_array($_separators) || !is_array($_out)) { return false; } if(empty($_separators)) { // One could replace this part with a call back function // to be applied to each separated string // {{{ Start call back function (a.k.a normalizer strategy) $data = strtolower(trim($_subject)); if($data !== '') { if(isset($_out[$data])) { $_out[$data] += 1; } else { $_out[$data] = 1; } } // End call back function }}} return true; } // Get the current separator list($key,$currentSeparator) = each($_separators); if(!is_string($currentSeparator)) { return false; } // Remove the current separator from the list unset($_separators[$key]); // Split the input by the current separator $explodedArray = explode($currentSeparator,$_subject); // Call the explode function for each string with the remaining // separators foreach($explodedArray as $string) { if($this->explode($string,$_separators,$_out) === false) { return false; } } return true; } } --------------------------------------------------------------- $strFunctions =& new strFunctions(); var_dump($strFunctions->explode($subject,$separators,$out)); var_dump($out); Output: bool(true) array(11) { ["this"]=> int(1) ["is"]=> int(2) ["a"]=> int(1) ["test"]=> int(1) ["me"]=> int(2) ["ssage:"]=> int(1) ["here"]=> int(1) ["message"]=> int(1) ["yet"]=> int(1) ["another"]=> int(1) ["finished"]=> int(1) }

« previous php.notes (#117563) next »