note 67651 added to function.explode
| From: | nthitz at gmail dot com | Date: | Fri, 09 Jun 2006 02:52:00 +0000 |
| Subject: | note 67651 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114005@lists.php.net to get a copy of this message | ||
I needed a function that could explode a string based upon two separators. This seems to do the
trick.
<?php
/*
Explodes a string based on two separator
Main use is to break a string into separate words, using ' ' and '-' as
separators.
*/
function multiExplode($separator1,$separator2,$string){
$returnArray=array();
if(strpos($string,$separator2)!==false){ //not all strings contain '-' so we don't
want to do a pointless loop
$containsSep2=true;
}else{
$containsSep2=false;
}
if(strpos($string,$separator1)!==false){
$explosion=explode($separator1,$string);
}else{
$explosion=array($string);
}
if($containsSep2){
for($i=0; $i<count($explosion); $i++){
if(strpos($explosion[$i],$separator2)!==false){
$tempExplosion=explode($separator2,$explosion[$i]);
foreach($tempExplosion as $piece){
$returnArray[]=$piece;
}
}else{
$returnArray[]=$explosion[$i];
}
}
$final=$returnArray;
}else{
$final=$explosion;
}
while(in_array('',$final)){ //must do this loop incase there are empty parts of the
array.
$key=array_search('',$final);
unset($final[$key]);
}
return $final;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 69.129.199.106
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/note/edit/67651
Del: integrated -- http://master.php.net/note/delete/67651/integrated
Del: useless -- http://master.php.net/note/delete/67651/useless
Del: bad code -- http://master.php.net/note/delete/67651/bad+code
Del: spam -- http://master.php.net/note/delete/67651/spam
Del: non-english -- http://master.php.net/note/delete/67651/non-english
Del: in docs -- http://master.php.net/note/delete/67651/in+docs
Del: other reasons-- http://master.php.net/note/delete/67651
Reject -- http://master.php.net/note/reject/67651
Search -- http://master.php.net/manage/user-notes.php