Example: -this--is---a-test--
-this--is---a-test--
What I want: this-is-a-test
this-is-a-test
Thanks for any answers! :)
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.
preg_replace
trim