note 67651 deleted from function.explode by bjori
| From: | bjori@php.net | Date: | Fri, 09 Jun 2006 09:29:52 +0000 |
| Subject: | note 67651 deleted from function.explode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114108@lists.php.net to get a copy of this message | ||
Note Submitter: nthitz at gmail dot com
Reason: bad code
----
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;
}
?>