TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-15-2008, 09:48 PM   #1 (permalink)
The Wanderer
 
Tree's Avatar
 
Join Date: Jun 2008
Location: Atlanta, USA
Posts: 8
Thanks: 1
Tree is on a distinguished road
Default 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?
__________________
NCIDev.com

Last edited by Tree : 06-15-2008 at 10:43 PM.
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
Old 06-15-2008, 10:38 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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;
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-15-2008, 10:44 PM   #3 (permalink)
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
Old 06-15-2008, 11:02 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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;
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-15-2008, 11:06 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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

Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tree (06-15-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:47 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design