使用 preg_split 拆分字符串

Splitting string using preg_split

我正在尝试使用 preg_split 拆分字符串,但我想包含分隔符,而且我不想捕获空字符串。我该怎么做?

$inputX = "hello1.hello2.";
$tempInput = preg_split( "~(\?|\.|!|\,)~", $inputX); //split the input at .!?,
print_r($tempInput)

结果:

Array ( [0] => hello1 [1] => hello2 [2] => )

需要结果:

Array ( [0] => hello1. [1] => hello2.

使用这个正则表达式:

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

Regex live here.

解释:

(?<=          # looks for positions after
    [.!?]     # one of these three characters
)             #
(?!           # but not
    $         # at the end
    |         # OR
    [.!?]     # before one of these three characters
 )            #

希望对您有所帮助。