PHP:用逗号(,)分割字符串,但忽略方括号内的任何内容? [英] PHP: Split a string by comma(,) but ignoring anything inside square brackets?
本文介绍了PHP:用逗号(,)分割字符串,但忽略方括号内的任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何用,
拆分字符串,但是跳过数组内部的字符串
How do I split a string by ,
but skip the one that's inside an array
字符串-'==',['abc','xyz'],1"
String - "'==', ['abc', 'xyz'], 1"
当我执行explode(',', $expression)
时,它会给我4个数组
When I do explode(',', $expression)
it's giving me 4 item in array
array:4 [
0 => "'=='"
1 => "['abc'"
2 => "'xyz']"
3 => 1
]
但是我希望输出为-
array:3 [
0 => "'=='"
1 => "['abc', 'xyz']"
2 => 1
]
推荐答案
是的,正则表达式-选择所有逗号,忽略方括号
yeah, regex - select all commas, ignore in square brakets
/[,]+(?![^\[]*\])/g
这篇关于PHP:用逗号(,)分割字符串,但忽略方括号内的任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文