在php中将字符串拆分为单词

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)}]
)
,(?![^{]*})(?![^(]*\))(?![^\[]*\])

You can use this.See demo.

https://regex101.com/r/lR1eC9/8

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.

DEMO

preg_split('~,(?![^{]*}|[^(]*\)|[^\[]*\])~', $string)