phpdelimiterpreg-split

use preg_split but keep delimiter


I read other related posts, but impossible to make it works in my case.

I have the following text :

This. Is a test. I show. You.

I want to use preg_split using delimiter '. ' (dot + space), but i need to keep delimiter in returned array. Here is the needed result :

array(
    '0' => 'This.',
    '1' => 'Is a test.',
    '2' => 'I show.',
    '3' => 'You.',
);  

What i've already tried :

preg_split("/(\.\s/)", $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

Solution

  • Use a zero-width assertion (a lookbehind here):

    $result = preg_split('~(?<=\.)\s~', $text, -1, PREG_SPLIT_NO_EMPTY);
    

    or you can use the \K feature that removes all on the left from the whole match:

    $result = preg_split('~\.\K\s~', $text, -1, PREG_SPLIT_NO_EMPTY);
    

    Without regex (if whitespaces are only spaces, and if the last dot is not followed by a space):

    $chunks = explode('. ', $text);
    $last = array_pop($chunks);
    $result = array_map(function ($i) { return $i . '.'; }, $chunks);
    $result[] = $last;
    

    or better:

    $result = explode(' #&§', strtr($text, ['. '=>'. #&§']));