tags:

views:

105

answers:

5
+3  Q: 

How to split value

Hi, I want to split value.

$value = "code1.code2.code3.code4(code5.code6(arg1.arg2, arg3), code7.code8)";

I want to split like this.

Array
(
    [0] => code1
    [1] => code2
    [2] => code3
    [3] => code4(code5.code6(arg1.arg2, arg3), code7.code8)
)

I used explode('.', $value) but explode split in parentheses value. I don't want split in parentheses value. How can i do?

A: 

can you use something other than '.' to separate the codes you want to split on? Otherwise, you require a regex replace.

$value = "code1|code2|code3|code4(code5.code6(arg1.arg2, arg3), code7.code8)";
$array = explode('|', $value);

Array
(
    [0] => code1
    [1] => code2
    [2] => code3
    [1] => code4(code5.code6(arg1.arg2, arg3), code7.code8)
)
Matthew Vines
I have to use dot(.)
scopus
+1  A: 

explode has a limit parameter:

$array = explode('.', $value, 4);

http://us.php.net/manual/en/function.explode.php

webbiedave
probably the easiest if the strings are fixed
Gordon
I'm certainly hoping they are! :)
webbiedave
I don't know limit value.
scopus
A: 

I think this'll work:

function split_value($value) {
    $split_values = array();
    $depth = 0;

    foreach (explode('.', $value) as $chunk) {
        if ($depth === 0) {
            $split_values[] = $chunk;
        } else {
            $split_values[count($split_values) - 1] .= '.' . $chunk;
        }

        $depth += substr_count($chunk, '(');
        $depth -= substr_count($chunk, ')');
    }

    return $split_values;
}

$value = "code1.code2.code3.code4(code5.code6(arg1.arg2, arg3), code7.code8).code9.code10((code11.code12)).code13";

var_dump(split_value($value));
beamrider9
+3  A: 

You need preg_match_all and a recursive regular expression to handle nested parethesis

$re = '~( [^.()]* ( ( ( [^()]+ | (?2) )* ) ) ) | ( [^.()]+ )~x';

  $re = '~( [^.()]* ( \( ( [^()]+ | (?2) )* \) ) ) | ( [^.()]+ )~x';

test

 $value = "code1.code2.code3.code4(code5.code6(arg1.arg2, arg3), code7.code8).xx.yy(more.and(more.and)more).zz";

 preg_match_all($re, $value, $m, PREG_PATTERN_ORDER);
 print_r($m[0]);

result

[0] => code1
[1] => code2
[2] => code3
[3] => code4(code5.code6(arg1.arg2, arg3), code7.code8)
[4] => xx
[5] => yy(more.and(more.and)more)
[6] => zz
stereofrog
+1 Nice--------
Felix Kling
This doesn't quite match the output he's looking for. Check out "code4".
beamrider9
*~*~*Mind.blown.*~*~*
notJim
Yup. Nice, indeed, though completely unreadable (to me) ;)
Gordon
@beamrider9: indeed. Answer updated.
stereofrog
Excellent work. I want to like this. Thanks
scopus
A: 

A simple parser:

$string = "code1.code2.code3.code4(code5.code6(arg1.arg2, arg3), code7.code8)code1.code2.code3.code4(code5.code6(arg1.arg2, arg3), code7.code8)";
$out = array();
$inparen = 0;
$buf = '';
for($i=0; $i<strlen($string); ++$i) {
    if($string[$i] == '(') ++$inparen;
    elseif($string[$i] == ')') --$inparen;

    if($string[$i] == '.' && !$inparen) {
        $out[] = $buf;
        $buf = '';
        continue;
    }
    $buf .= $string[$i];

}
if($buf) $out[] = $buf;
Rob