如果在“”之后有任何字符";然后按<;空格>;他们之间


If there is any character after "," then press <space> between them

我有一些字符串如下:

$str = "it is a test,it is a test";
$str = "it is a test ,it is a test";
$str = "it is a test, it is a test";
$str = "it is a test , it is a test";

现在我想要他们所有人都这样:

$str = "it is a test, it is a test";

现在,我可以通过几个步骤做到这一点:

  1. str_replace(" , ",",","$str");
  2. str_replace(" ,",",","$str");
  3. str_replace(", ",",","$str");
  4. str_replace(",",", ","$str");

然后输出将是我想要的。现在我想知道,有没有REGEX代码可以一步到位?

您可以使用它并将其替换为,[space]

's*,'s*

即:

preg_replace('/'s*,'s*/', ', ', $str);