我正在尝试使用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)
预期成绩:
为什么我没有钱
实际结果:
为什么会
您需要转义特殊的正则表达式字符(.和?)并删除空格.
<?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)
输出:
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
| 归档时间: |
|
| 查看次数: |
2072 次 |
| 最近记录: |