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

后端 未结 2 1972
我在风中等你
我在风中等你 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:41

    Without regular expression....

    $string = trim($string,'-');
    
    while (stristr($string,'--')) {$c = str_ireplace('--','-',$string);}
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题