按不在括号内的点分割
我想分割价值。
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要 preg_match_all 和递归正则表达式来处理嵌套括号
$re = '~( [^.()]* ( ( ( [^()]+ | (?2) )* ) ) ) | ( [^.()]+ )~x';测试
结果
You need preg_match_all and a recursive regular expression to handle nested parethesis
$re = '~( [^.()]* ( ( ( [^()]+ | (?2) )* ) ) ) | ( [^.()]+ )~x';test
result
explode 有一个限制参数:
http://us.php.net/manual/ en/function.explode.php
explode has a limit parameter:
http://us.php.net/manual/en/function.explode.php
您可以使用“.”以外的其他内容吗?分离您想要拆分的代码?否则,您需要进行正则表达式替换。
can you use something other than '.' to separate the codes you want to split on? Otherwise, you require a regex replace.
我认为这会起作用:
I think this'll work:
一个简单的解析器:
A simple parser: