Reputation: 11
I want to split string which contains braces e.g.
string = "some-thing_text,text in rounded brackets(word first,word second),Text in curly brackets{some-text(some one,some two),some another},Text in square brackets[some text,some another{some like this(this1,this2)}]"
and output will be :
Array
(
[0] => some-thing_text
[1] => text in rounded brackets(word first,word second)
[2] => Text in curly brackets{some-text(some one,some two),some another}
[3] => Text in square brackets[some text,some another{some like this(this1,this2)}]
)
Upvotes: 0
Views: 189
Reputation: 174874
You may try this,
preg_split('~(?:\[.*?\]|\(.*?\)|\{.*?\})(*SKIP)(*F)|,~', $str);
(?:\[.*?\]|\(.*?\)|\{.*?\})
matches all the bracketed blocks.(*SKIP)(*F)
makes the previous match to fail.,
Now it matches comma from the remaining string.Upvotes: 1
Reputation: 67998
,(?![^{]*})(?![^(]*\))(?![^\[]*\])
You can use this.See demo.
https://regex101.com/r/lR1eC9/8
Upvotes: 3