Working on the text:
This is some text
which I am working on.
This text has whitespace before the new line but after this word
Another line.
I am using preg_split to split on the unicode whitespace and all special characters excluding newline like so:
preg_split("/\p{Z}|[^\S\n]/u", $data, -1, PREG_SPLIT_OFFSET_CAPTURE);
The flag is because I absolutely need to retain the positions of the strings.
I would like to have the preg_split keep the newlines with their preceeding word. For example the newline can appear at the beginning of the following word, or even on their own.
Expected output when working correctly:
This
is
some
text\n
which
I
am
working
on.\n
This
text
has
whitespace
before
the
new
line
but
after
this
word\n
Another
line.
Can anyone explain how this might be achieved? Thanks
Use a lookbehind to match the boundary which exists after newline character.
Output: