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);}