开发者

Replace Multiple "-" PHP [duplicate]

开发者 https://www.devze.com 2022-12-19 01:20 出处:网络
This question already has answers here: Closed 12 years ago. Possible Du开发者_如何学编程plicate:
This question already has answers here: Closed 12 years ago.

Possible Du开发者_如何学编程plicate:

PHP Preg-Replace more than one underscore

Hi, I'm just wondering how I can replace 2 or more - signs in a string with just one in PHP.

So like

1-2---3--4

would go to

1-2-3-4

Thanks :)


Use preg_replace:

$str = preg_replace('/-+/', '-', $str);


No need regex

$text="1-2---3--4";
$s = implode("-",array_filter ( explode("-",$text) )) ;
print_r($s);
0

精彩评论

暂无评论...
验证码 换一张
取 消