小编Chr*_*man的帖子

在问号和感叹号之前添加空格

我的客户希望在惊叹号和问号之前留一个空格.为了确保始终正确完成,我在php中使用以下脚本.此脚本删除所有现有空格,然后在所有问号和感叹号之前放置一个不间断的空格:

$text =  str_replace(' ?', '?', $text);
$text = str_replace('?', ' ?', $text);
$text = str_replace(' !', '!', $text);
$text = str_replace('!', ' !', $text);
return $text;
Run Code Online (Sandbox Code Playgroud)

一切正常,但我想知道Regex是否有更好的方法?

php regex

0
推荐指数
1
解决办法
407
查看次数

标签 统计

php ×1

regex ×1