可能重复:
PHP Preg-Replace more than one underscore替代多个 “ - ” PHP
嗨,我只是想知道我怎么能代替2个或更多 - 只需一个在标志中的字符串PHP。
所以像
1-2 --- 3-4
会去
1-2-3-4
谢谢:)
可能重复:
PHP Preg-Replace more than one underscore替代多个 “ - ” PHP
嗨,我只是想知道我怎么能代替2个或更多 - 只需一个在标志中的字符串PHP。
所以像
1-2 --- 3-4
会去
1-2-3-4
谢谢:)
使用preg_replace:
$str = preg_replace('/-+/', '-', $str);
或更好的'$海峡的副本=的preg_replace(”/- {2,} /',' - ',$ str);' – raveren
@Reveren这不会错过---在字符串中间吗? –
当然,从技术上将recude替换数但恕我直言的差别可以忽略和'/ - + /'很多清洁:) –
无需正则表达式
$text="1-2---3--4";
$s = implode("-",array_filter (explode("-",$text))) ;
print_r($s);
爆炸数组,然后加入回的字符串比正则表达式版本既不更加清晰,也没有有效的。如果这就是您的想法,请致电 – kennytm
。但不是我。 – ghostdog74
这是http://stackoverflow.com/questions/1729500/php-preg-replace-more-than-one-underscore – soulmerge