按不在括号内的点分割

发布于 2024-08-26 07:14:21 字数 363 浏览 11 评论 0原文

我想分割价值。

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

我想就这样分开。

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

我使用explode('.', $value),但explode也在括号值中分割。我不想在括号表达式内进行拆分。 我该怎么办?

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 splits in parentheses value too. I don't want to split inside parenthetical expressions.
How can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆梦 2024-09-02 07:14:21

您需要 preg_match_all 和递归正则表达式来处理嵌套括号

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

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

测试

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

结果

[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

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
爱你不解释 2024-09-02 07:14:21

explode 有一个限制参数:

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

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

explode has a limit parameter:

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

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

梦醒灬来后我 2024-09-02 07:14:21

您可以使用“.”以外的其他内容吗?分离您想要拆分的代码?否则,您需要进行正则表达式替换。

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

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)
)
水晶透心 2024-09-02 07:14:21

我认为这会起作用:

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

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));
凉城凉梦凉人心 2024-09-02 07:14:21

一个简单的解析器:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文