How do I, using preg_replace, replace more than one underscore with just one underscore?
The + operator matches multiple instances of the last character (or capture group).
+
$string = preg_replace('/_+/', '_', $string);