You could perhaps use preg_match_all
instead?
$fileString = '[sectionone]
...
...
[sectiontwo]
...
...
[sectionthree]
...
...';
preg_match_all("/^\[.*?(?=\n\[|\z)/ms", $fileString, $matches);
print_r($matches);
This will match [
till it finds a \n
followed by a [
or at the end of the string. The flags ms
are important here to make ^
match the beginning of all lines and for .
to match newlines.
Or with splitting...
print_r(preg_split("/\n(?=\[)/", $fileString));
This will match a \n
only if followed by a [
.