How do I, using preg_replace, replace more than one underscore with just one underscore?
See Question&Answers more detail:osHow do I, using preg_replace, replace more than one underscore with just one underscore?
See Question&Answers more detail:osThe +
operator (quantifier) matches multiple instances of the last character (, character class or capture group or back-reference).
$string = preg_replace('/_+/', '_', $string);
This would replace one or more underscores with a single underscore.
Technically more correct to the title of the question then is to only replace two or more:
$string = preg_replace('/__+/', '_', $string);
Or writing the quantifier with braces:
$string = preg_replace('/_{2,}/', '_', $string);
And perhaps then to capture and (back-) reference:
$string = preg_replace('/(_)1+/', '1', $string);