我有一些字符串如下:
$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";
现在,我可以通过几个步骤做到这一点:
str_replace(" , ",",","$str");
str_replace(" ,",",","$str");
str_replace(", ",",","$str");
str_replace(",",", ","$str");
然后输出将是我想要的。现在我想知道,有没有REGEX代码可以一步到位?
您可以使用它并将其替换为,[space]
s*,s*
即:
preg_replace('/s*,s*/', ', ', $str);