I have this simple pattern that splits a text into periods:
$text = preg_split("/[.:!?]+/", $text);
But I want to include . :
or !
at the end of the array items.
That is, now for "good:news.everyone!" I have:
array("good", "news", "everyone", "");
But I want:
array("good:", "news.", "everyone!", "");
Here you go:
How it works: The pattern actually turns everything into a delimiter. Then, to include these delimiters in the array, you can use the
PREG_SPLIT_DELIM_CAPTURE
constant. This will return an array like:To get rid of the empty values, use
PREG_SPLIT_NO_EMPTY
. To combine two or more of these constants, we use the bitwise|
operator. The result: