Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace multiple occurrences of same symbol using preg_replace?

Let's say I have a string like this:

$string = "hello---world";

How would I go about replacing the --- with a single hyphen? The string could easily look like this instead:

$string = "hello--world----what-up";

The desired result should be:

$string = "hello-world-what-up";
like image 863
kasperwf Avatar asked Jul 22 '10 13:07

kasperwf


1 Answers

$string = preg_replace('/-{2,}/','-',$string);
like image 144
Mark Baker Avatar answered Oct 14 '22 18:10

Mark Baker