2012-07-18 85 views
2

删除的空间,我有以下字符串+ Example + Test + Test2和使用的preg_replace我希望得到以下结果+Example +Test +Test2在PHP中使用的preg_replace在PHP

我一直在尝试使用下面的代码

preg_replace("/\s*([\+])\s*/", "$1", $string) 

但是这一个是返回

+Example+Test+Test2 

回答

5
$output = str_replace('+ ', '+', $input); 

$output = preg_replace('/\\+\\s+/', '+', $input); 
+0

那么+之前的多个空格呢?我知道Q并没有这样说(但它暗示了它)。只是调皮:) – Pete 2012-07-18 13:36:41

+0

$ output = trim(preg_replace('/ \ s * + \ s * /','+',$ input)); – Ron 2012-07-18 13:38:22