How can I convert two or more dashes to singles and remove all dashes at the beginning and end of a string?

后端 未结 2 1973
我在风中等你
我在风中等你 2020-12-06 19:15

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

What I want: this-is-a-test

Thanks for any answers! :)

2条回答
  •  天命终不由人
    2020-12-06 19:56

    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.

提交回复
热议问题