2

I have strings like this :

Aaaaaaaaa

I want to stop repeating just to 3 times, but not break the lowercase/uppercase, like this :

Aaa

My regex breaks the lowercase/uppercase :

$patternReplace = '/(.)\1{3,}/iu';    
$chaine = preg_replace($patternReplace, '$1$1$1', $chaine, -1 );

Result :

AAA

I want to get :

Aaa

thanx for help

0

1 Answer 1

1

Use subpatterns to get the additional backreferences(first subpattern is for the first character, the second subpattern - for the next two identical characters) :

$chain = "Aaaaaaaaa";
$patternReplace = '/(.)(\1{2})\1{1,}/iu';
$chain = preg_replace($patternReplace, '$1$2', $chain);

print_r($chain);  // "Aaa"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.