Explode a string in php excluding the ', ' within braces
Asked Answered
E

2

3

I have a string of this-

$str = "field1.id as field1, 
       DATE_SUB(field2, INTERVAL (DAYOFMONTH(field2)-1) DAY) as field2,   
       field3.name as field3";

Need to explode this into array with , as this:

$requiredArray = array(
  0 => field1.id as field1,
  1 => DATE_SUB(field2, INTERVAL (DAYOFMONTH(field2)-1) DAY) as field2
  2 => field3.name as field3
);

I've tried with explode but it doesn't works:

$requiredArray = explode(', ', $str); 
// doesn't work as "DATE_SUB(field2, INTERVAL ..." also gets exploded

Any trick/ideas?

Ebersole answered 12/5, 2016 at 10:15 Comment(11)
You'll need to use a preg_split that can test if your comma is inside braces and only split if it isn't. Or perhaps something like preg_match_all('#\(.*?\)|[^,]+#', $str, $matches); var_dump($matches[0]);Shah
you need regular expression.Sincere
Nice clue, Thanks, I'll give it a try. +1Ebersole
@MarkBaker That doesn't work for now :(.Ebersole
The principle of using a regexp should still apply, it's just getting the right regexp that is a problem; I'll see what I can findShah
This was not as obvious as I thought. Can anyone please help me out here ? Not working after several attempts :(Ebersole
Maybe just use explode("\n", $str)? By line break, not by comma.Janise
That is not a line break its a space, I've wrapped it just to make the question clear to displayEbersole
take a look at: https://3v4l.org/OVHQhSincere
@FrayneKonok Yeah, I've checked this while googling it over, unfortunately this regex doesn't work in my case, may be need to modify it a littleEbersole
I also surprised about it, But taking time to solve it.Sincere
L
4

Please try this

$str = "field1.id as field1, 
       DATE_SUB(field2, INTERVAL (DAYOFMONTH(field2)-1) DAY) as field2,   
       field3.name as field3";

$buffer = '';
$stack = array();
$depth = 0;
$len = strlen($str);
for ($i=0; $i<$len; $i++) {
    $char = $str[$i];
    switch ($char) {
    case '(':
        $depth++;
        break;
    case ',':
        if (!$depth) {
            if ($buffer !== '') {
                $stack[] = $buffer;
                $buffer = '';
            }
            continue 2;
        }
        break;
    case ' ':
        if (!$depth) {
             $buffer .= ' ';
            continue 2;
        }
        break;
    case ')':
        if ($depth) {
            $depth--;
        } else {
            $stack[] = $buffer.$char;
            $buffer = '';
            continue 2;
        }
        break;
    }
    $buffer .= $char;
}
if ($buffer !== '') {
    $stack[] = $buffer;
}
echo "<pre>";
print_r($stack);
echo "</pre>";
Lordling answered 12/5, 2016 at 11:54 Comment(2)
Yeah, I don't think regex can do this now. Although its a bigger soln, I'll go with it. Thanks!Ebersole
#4538586 works well too :)Ebersole
S
-1

Try this :

preg_match_all('~(.*)[^(],?~', $str, $matches);
Skiba answered 12/5, 2016 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.