php to replace multiple dashes to single dash

In this blog we’re going to know how to replace multiple dashes to one single dash

let’s assume we have a string like

something-------another--thing

And want to convert this string into

something-another-thing

Here is the top Answer to replace multiple dashes to single dash

<?php
$str='something-------another--thing';
$str = preg_replace('/-+/', '-', $str);
?>

Output

something-another-thing

Leave a Comment