I'm trying to explode a string by vertical bars. That's the easy part. However, I DON'T want the split to affect substrings that are surrounded by parentheses. That means I need a string such as:
Hello (sir|maam).|Hi there!
to explode into:
Array
(
[0] => Hello (sir|maam).
[1] => Hi there!
)
By using the normal explode function, I don't believe there is a way to tell it to ignore that bar surrounded by the parentheses. However, I have some ideas.
I know that it would be possible to do this by exploding the string normally, and then looping through the array and merging everything between strings that contain (
to the closing string that contains )
. However, I have a feeling that there should be a more elegant way of achieving this.
Am I right? Is there a less code-intensive means of spliting a string into an array given these restrictions?