繁体   English   中英

字符串替换为多次

[英]String replace with multiple occurrences

我有以下字符串

Re: Re: Re: Re: Re: Re: Re: Re:

我想要的是更改此字符串的最终结果是:

Re:

因此,我正在寻找一种检测多个Re:之间有一个空格)并将其更改为一个Re:

如果我有任何想法如何做到这一点,我会发布一些代码,但没有真正的线索如何在php做到这一点

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

<?php
$s = "Re: Re: Re: Re: Re: Re: Re: Re: Prueba";
$s = preg_replace("/(Re: ?){2,}/i", "Re: ", $s);
var_dump($s); // Re: Prueba

说明:

(Re: ?) is just Re: and an optional space between them.
{2,}    means 2 or more times (if it's just one, why replace it)
i       so it's case insensitive

演示版

暂无
暂无

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

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