如果","后面有任何字符,请在<space>它们之间按



我有一些字符串如下:

$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);

最新更新