2012-02-17 66 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

相关问题