在问号,感叹号或句点处拆分字符串

jes*_*ica 3 php preg-split

我正在尝试使用preg_split将字符串拆分为问号,感叹号或句号,但我遇到了问题.它不是在问号处分裂,而是在此之前拆分字符串.请看一下我的代码:

<?php

    $input = "Why will I have no money? Because I spent it all";
    $input = preg_split( "/ (?|.|!) /", $input ); 
    $input = $input[0];
    echo $input;

?>
Run Code Online (Sandbox Code Playgroud)

预期成绩:

为什么我没有钱

实际结果:

为什么会

chr*_*s85 6

您需要转义特殊的正则表达式字符(.?)并删除空格.

<?php
$input = "Why will I have no money? Because I spent it all";
    $input = preg_split( "/(\?|\.|!)/", $input ); 
print_r($input);
Run Code Online (Sandbox Code Playgroud)

演示:https://eval.in/422337

输出:

Array
(
    [0] => Why will I have no money
    [1] =>  Because I spent it all
)
Run Code Online (Sandbox Code Playgroud)

Regex101演示:https://regex101.com/r/zK7cK7/1