Edit: OK, I can’t read, thanks to Col. Shrapnel for the help. If anyone comes here looking for the same thing to be answered…
print_r(preg_split('/([\!|\?|\.|\!\?])/', $string, null, PREG_SPLIT_DELIM_CAPTURE));
Is there any way to split a string on a set of delimiters, and retain the position and character(s) of the delimiter after the split?
For example, using delimiters of ! ? . !? turning this:
$string = 'Hello. A question? How strange! Maybe even surreal!? Who knows.';
into this
array('Hello', '.', 'A question', '?', 'How strange', '!', 'Maybe even surreal', '!?', 'Who knows', '.');
Currently I’m trying to use print_r(preg_split('/([\!|\?|\.|\!\?])/', $string)); to capture the delimiters as a subpattern, but I’m not having much luck.
Your comment sounds like you’ve found the relevant flag, but your regex was a little off, so I’m going to add this anyway:
Note that this will leave spaces at the beginning of every string after the first, so you’ll probably want to run them all through
trim()as well.Results: