3

I have strings like this one:

$str = 'This -----is a bbbb test';

How can I remove all duplicate characters if it occurs more than 3 times?

So, for example, the string above must look as follows:

'This is a  test';

2 Answers 2

7

You can do this using regular expressions and preg_replace():

$new_str = preg_replace('/(.)\1{3,}/', '', $str);
Sign up to request clarification or add additional context in comments.

1 Comment

Hm, this doesn't work. I use different regex which does the trick: preg_replace('{([^\w])\1+}','',$str);
1
$t = preg_replace('/(\S)\1{3,}/', '', $t);

Every non-space longer than 3 chars will be replaced with nothing

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.