TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Picky explode() (http://www.talkphp.com/general/2964-picky-explode.html)

Tree 06-15-2008 09:48 PM

Picky explode()
 
I was browsing another forum earlier today and came across someone who wanted to split a string into an array. That's a fairly simple task with explode(). But they also wanted anything in between brackets to be left alone.

I came up with a solution that works fine, but I'm fairly certain there's a better way to do it. Here's what I've got.

PHP Code:

<?php
$string 
"501,Red_Potion,Red Potion,0,50,,70,,,,,0xFFFFFFFF,7,2,,,,,,{ itemheal rand(45,65),0; },{},{}";
echo 
'<pre>';
print_r(picky_explode($string));
echo 
'</pre>'

function 
picky_explode($string) {
    
$array explode(',',$string);
    
$array_size count($array);
    foreach (
$array as $key => $value) {
        
$newvalue '';
        
// The code below combines any array elements between a pair of brackets {}
        
if (strpos($value,'{') !== false) {
            for (
$i $key;$i<=$array_size;$i++) {
                
$newvalue .= ",".$array[$i];
                if (
strpos($array[$i],'}') !== false) {
                    for (
$x=$i;$x>$key;$x--) {
                        
$kill[] = $x;
                    }
                    break;
                }
            }
        } else {
            
$newvalue $value;
        }
        
$array2[] = str_replace(",{""{",$newvalue);
    }
    foreach (
$kill as $value) {
        unset(
$array2[$value]);
    }
    foreach (
$array2 as $value) {
        
$final_array[] = $value;
    }
    return 
$final_array;
}
?>

Any ideas?

Wildhoney 06-15-2008 10:38 PM

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;
}

Tree 06-15-2008 10:44 PM

Quote:

Originally Posted by Wildhoney (Post 15694)
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] => {}
)


Wildhoney 06-15-2008 11:02 PM

Yes, sorry, my bad. I forgot to put a line in a conditional statement. Try:

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;
    $aParts = explode($szSplitBy, $szString);
    $aReturn = array();
   
    foreach ($aParts as $szPart)
    {
        if (!$bInBrackets)
        {
            $aReturn[] = $szPart;
        }
       
        if (substr($szPart, 0, 1) == '{')
        {
            $bInBrackets = true;
            continue;
        }
       
        if ($bInBrackets)
        {
            $aReturn[count($aReturn) - 1] .= $szSplitBy . $szPart;
        }
       
        if (substr($szPart, -1, 1) == '}')
        {
            $bInBrackets = false;
        }
    }
   
    return $aReturn;
}

Salathe 06-15-2008 11:06 PM

Not too sure about using preg_split but here's a possible regex solution:

PHP Code:

function regex_explode($string)
{
    
preg_match_all('/
        # Previous character is start of string or a comma
        (?<=^|,)
        #Followed by
        (?:
            # Zero or more non-curly brace characters between curly braces
            {[^{}]*}
            # or 
            |
            # Zero or more non-comma characters
            [^,]*
        )
    /x'
$string$matches);
    
    
// preg_match_all('/(?<=^|,)(?:{[^{}]*}|[^,]*)/', $string, $matches);
    
    
return current($matches);




All times are GMT. The time now is 04:41 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0