6

Example: -this--is---a-test--

What I want: this-is-a-test

Thanks for any answers! :)

Gumbo
  • 620,600
  • 104
  • 758
  • 828
stunnaman
  • 891
  • 3
  • 12
  • 19

2 Answers2

21

I would use a combination of preg_replace and trim:

trim(preg_replace('/-+/', '-', $str), '-')

The preg_replace call removes multiple dashes and trim removes the leading and trailing dashes.

Gumbo
  • 620,600
  • 104
  • 758
  • 828
0

Without regular expression....

$string = trim($string,'-');

while (stristr($string,'--')) {$c = str_ireplace('--','-',$string);}
John
  • 11,516
  • 11
  • 87
  • 151