 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
06-15-2008, 09:48 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Atlanta, USA
Posts: 8
Thanks: 1
|
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?
Last edited by Tree : 06-15-2008 at 10:43 PM.
|
|
|
06-15-2008, 10:38 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
06-15-2008, 10:44 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Jun 2008
Location: Atlanta, USA
Posts: 8
Thanks: 1
|
Quote:
Originally Posted by Wildhoney
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] => {}
)
|
|
|
06-15-2008, 11:02 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
06-15-2008, 11:06 PM
|
#5 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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);
}
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|