使用正则表达式preg_replace用三个句点替换空格和三个句点?

时间:2017-09-20 17:13:18

标签: php regex

试图让这个工作,但我的正则表达式生锈,没有任何我谷歌搜索似乎工作

<?php echo preg_replace("/\s+/u", '...', $post->getPostExcerpt(12)) ?>

这确实用一个......替换了所有空格,但我想要做的就是用三个点替换一个空格和三个点。

我尝试过str_replace,它没有占用空间,所以我试图用preg_replace来获取它。

/\s+/u+(...)

以上只是用点代替每个字符。

我错过了什么?

3 个答案:

答案 0 :(得分:1)

尝试:

<?php echo preg_replace("/\s\.{3}/u", '...', $post->getPostExcerpt(12)) ?>

https://regex101.com/r/1KXvs9/2

或只是

str_replace(" ...","...",$post->getPostExcerpt(12));

答案 1 :(得分:1)

请勿使用正则表达式进行此简单替换。这会将space...替换为...

echo str_replace(' ...', '...', $post->getPostExcerpt(12));

使用正则表达式,您可以使用:

echo preg_replace('/ [.]{3}/', '...', $post->getPostExcerpt(12));

答案 2 :(得分:0)

我要做的是用2014-03-04 21:35:31 替换a space three dots

three dots

OR

$search = " ...";
$replace= '...';
print str_replace($search, $replace, $post->getPostExcerpt(12));

请参阅正则表达式:https://regex101.com/r/paAg7h/2

相关问题