如何删除多个逗号,这些逗号一起使用php,逗号之间没有空格

时间:2018-05-07 12:51:15

标签: php regex replace preg-replace comma

我试图删除使用php一起使用的多个逗号。这是代码:

$var = "Hello, welcome, when,,,,,,, ."
$str = preg_replace('/,+/', '', $var);

显示输出为:

 Hello  welcome when    .

但预期产出是:

 Hello, welcome, when.

1 个答案:

答案 0 :(得分:0)

您正在替换''。你应该这样做:

private string CreateGraphicString() {
     StringBuilder builder = new StringBuilder();
    int row, column;

  for (row = 0; row <= 6; row++) {
    for (column = 0; column <= 6; column++) {
      if ((column == 1 && row != 0 && row != 6) || 
          ((row == 0 || row == 6) && column > 1 && column < 5) || 
           (row == 3 && column > 2 && column < 6) || 
           (column == 5 && row != 0 && row != 2 && row != 6))
        builder.Append("*");
      else
        builder.Append(" ");
    }

    builder.AppendLine();
  }
  builder.AppendLine();
}
 return builder.ToString();
}

myLabel.Text = builder.AppendLine();
myTextbox.Text = builder.AppendLine();

其中有一个或多个 preg_replace('/,+/', ',', $var); 替换为一个,

,