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
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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
 



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 01:32 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