【问题标题】:How can I convert two or more dashes to singles and remove all dashes at the beginning and end of a string?如何将两个或多个破折号转换为单行并删除字符串开头和结尾的所有破折号?
【发布时间】:2010-10-18 23:58:48
【问题描述】:

示例:-this--is---a-test--

我想要的:this-is-a-test

感谢您的任何回答! :)

【问题讨论】:

    标签: php regex


    【解决方案1】:

    没有正则表达式....

    $string = trim($string,'-');
    
    while (stristr($string,'--')) {$c = str_ireplace('--','-',$string);}
    

    【讨论】:

      【解决方案2】:

      我会使用preg_replacetrim 的组合:

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

      preg_replace 调用删除多个破折号,trim 删除前导和尾随破折号。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        • 2020-01-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多