D
D
DERBIAGLOBALISTO2019-11-25 10:55:51
Design
DERBIAGLOBALISTO, 2019-11-25 10:55:51

How to replace more than two repeated characters in a row using regular expressions?

For example, there is a number "12225422642". It is necessary that only the first three deuces be replaced by one deuce.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
0
0xD34F, 2019-11-25
@0xD34F

preg_replace('/(.)\1{2,}/', '\1', $str)

A
Anton Shamanov, 2019-11-25
@SilenceOfWinter

if for some reason you don't want to learn regular expressions, then what prevents you from using the while loop (strpos($str, '222') !== false) ?

E
Edward, 2019-11-25
@IS-Builder

DERBIAGLOBALISTO

$str = '1222542264222';

$str = preg_replace('~(2)\1{2,}~', '$1', $str);

echo $str;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question