2012-07-18 79 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