2017-08-23 16 views
1

I持って次の文字列文字列が複数回出現して置き換える

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

私が欲しいのです、この文字列を変異の最終的な結果は次のとおりです。

Re: 

それでは、私が探していますがへの道でありますRe:(スペースを挟んで)を複数検出し、1つだけRe:に変更してください。

私はphp

答えて

8

でこれを行う方法をいくつかのコードが、本当の手がかりを投稿したいこれを実現するためにどのように任意のアイデアを持っていた場合は、この正規表現を使用することができます。

<?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 

Demo

関連する問題