Array(
[1] => put returns (between) paragraphs
[2] => (for) linebreak (add) 2 spaces at end
[3] => indent code by 4 (spaces!)
[4] => to make links
)
Want to get text inside brackets (for each value):
- take only first match
- remove this match from the value
- write all matches to new array
After function arrays should look like:
Array(
[1] => put returns paragraphs
[2] => linebreak (add) 2 spaces at end
[3] => indent code by 4
[4] => to make links
)
Array(
[1] => between
[2] => for
[3] => spaces!
[4] =>
)
What is the solution?
I would use the regular expression
/\((\([^()]*\)|[^()]*)\)/(this will match one or two pairs of parentheses) together withpreg_split:Using
preg_splitwith PREG_SPLIT_DELIM_CAPTURE flag set will contain the matched separators in the result array. So a match was found, there are at least three parts. In that case the second member is the one we are looking for. That member is removed witharray_splicethat does also return the array of removed members. To get the removed member,currentis used on the return value ofarray_splice. The remaining members are then put back together.