如何将两个或多个短划线转换为单个并删除字符串开头和结尾的所有短划线?

时间:2021-10-24 20:16:45

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

示例:-this - 是--- a-test--

What I want: this-is-a-test

我想要的是:这是一个测试

Thanks for any answers! :)

谢谢你的回答! :)

2 个解决方案

#1


18  

I would use a combination of preg_replace and trim:

我会使用preg_replace和trim的组合:

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

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

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

#2


0  

Without regular expression....

没有正则表达....

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

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

#1


18  

I would use a combination of preg_replace and trim:

我会使用preg_replace和trim的组合:

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

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

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

#2


0  

Without regular expression....

没有正则表达....

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

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