繁体   English   中英

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

[英]Replace a space and three periods with just three periods using regex preg_replace?

试图使它起作用,但是我的正则表达式生锈了,我正在谷歌搜索的东西似乎都没有起作用

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

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

我尝试了str_replace,但它没有占用空间,因此我想通过preg_replace来获取它。

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

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

我想念什么?

尝试:

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

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

要不就

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

不要使用正则表达式进行此简单替换。 这用space...代替space... ...

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

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

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

我想做的是用three dots替换a space three dots

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

要么

$re = '/\s\.{3}/u';
$str = $post->getPostExcerpt(12);
$subst = '...';

$result = preg_replace($re, $subst, $str);
echo $result;

见正则表达式: https : //regex101.com/r/paAg7h/2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM