note 69450 added to function.explode
| From: | gtatworkathotmaildotcom at osu1 dot php dot net | Date: | Wed, 06 Sep 2006 16:02:20 +0000 |
| Subject: | note 69450 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116765@lists.php.net to get a copy of this message | ||
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)
}
----
Server IP: 216.194.113.175
Probable Submitter: 66.235.39.39
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/69450
Del: integrated -- https://master.php.net/note/delete/69450/integrated
Del: useless -- https://master.php.net/note/delete/69450/useless
Del: bad code -- https://master.php.net/note/delete/69450/bad+code
Del: spam -- https://master.php.net/note/delete/69450/spam
Del: non-english -- https://master.php.net/note/delete/69450/non-english
Del: in docs -- https://master.php.net/note/delete/69450/in+docs
Del: other reasons-- https://master.php.net/note/delete/69450
Reject -- https://master.php.net/note/reject/69450
Search -- https://master.php.net/manage/user-notes.php