I'm trying to split a string using preg_split, but I want to include the delimiters, and I don't want to capture an empty string. How would I do this?
$inputX = "hello1.hello2.";
$tempInput = preg_split( "~(\?|\.|!|\,)~", $inputX); //split the input at .!?,
print_r($tempInput)
Result:
Array ( [0] => hello1 [1] => hello2 [2] => )
Need Result:
Array ( [0] => hello1. [1] => hello2.
Use this regex:
(?<=[.!?])(?!$|[.!?])
Explaining:
(?<= # looks for positions after
[.!?] # one of these three characters
) #
(?! # but not
$ # at the end
| # OR
[.!?] # before one of these three characters
) #
Hope it helps.