Thread: Picky explode()
View Single Post
Old 06-15-2008, 10:44 PM   #3 (permalink)
Tree
The Wanderer
 
Tree's Avatar
 
Join Date: Jun 2008
Location: Atlanta, USA
Posts: 8
Thanks: 1
Tree is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
You could probably do this with preg_split, if we were clever enough. However, I for one am not. There are a few on TalkPHP which I'm sure are. This would be my solution:

php Code:
$szString = "501,bleh,{ itemheal rand(45,65),0; },bleh,1,2,3,{ohh,1,2}";
print_r(picky_explode(',', $szString));

function picky_explode($szSplitBy, $szString)
{
    $bInBrackets = false;
    $aReturn = array();
    $aParts = explode($szSplitBy, $szString);
   
    foreach ($aParts as $szPart)
    {
        if (!$bInBrackets)
        {
            $aReturn[] = $szPart;
        }
       
        if (substr($szPart, 0, 1) == '{')
        {
            $bInBrackets = true;
            continue;
        }
       
        $aReturn[count($aReturn) - 1] .= $szSplitBy . $szPart;
       
        if (substr($szPart, -1, 1) == '}')
        {
            $bInBrackets = false;
        }
    }
   
    return $aReturn;
}
Returns odd results with the original string.

Code:
Array
(
    [0] => 501,501
    [1] => Red_Potion,Red_Potion
    [2] => Red Potion,Red Potion
    [3] => 0,0
    [4] => 50,50
    [5] => ,
    [6] => 70,70
    [7] => ,
    [8] => ,
    [9] => ,
    [10] => ,
    [11] => 0xFFFFFFFF,0xFFFFFFFF
    [12] => 7,7
    [13] => 2,2
    [14] => ,
    [15] => ,
    [16] => ,
    [17] => ,
    [18] => ,
    [19] => { itemheal rand(45,65),0; }
    [20] => {}
)
__________________
NCIDev.com
Send a message via ICQ to Tree Send a message via AIM to Tree Send a message via MSN to Tree Send a message via Yahoo to Tree
Tree is offline  
Reply With Quote