2012-02-17 67 views
-1

可能重複:
Replace comma + space with 「,」 but not spaces without commas逗號之前或之後刪除空格在PHP

這裏是我的文字

$x = 'first name, middle name, last name'; 

期望輸出

first name,middle name,last name 

試過這種

$x = str_replace(array(', ', ' ,', ' , '), array(',', ',', ','), $x); 

我不知道這樣的方式

+0

預期的行爲:告訴我們你已經嘗試了什麼。即使這是意想不到的,你會得到什麼結果? – 2012-02-17 19:15:11

+0

*我不確定這種方式*:它適合你嗎? – 2012-02-17 19:19:14

回答

11
$x = preg_replace('/\s*,\s*/', ',', $x); 
+0

如何在java中表達式 – 2012-12-14 02:47:27

+0

如果我想僅在逗號前刪除空格,它將會如何顯示? – bernte 2013-07-29 13:32:17

+0

刪除第二個'\ s *' – ThiefMaster 2013-07-29 15:03:11

相關問題