phpregexpreg-split

Split string on punctuation symbols without losing the symbols


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.

Solution

  • Use this regex:

    (?<=[.!?])(?!$|[.!?])
    

    Regex live here.

    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.