I’m having a string which is comma seperated like this
$str = "john, alice, mary,joy";
Some are having space after comma and some don’t. What I want to do is remove all the commas and make them like this:
$str = "john alic mary joy";
What is the best way to do this in php?
str_replaceis the simplest solution when there is at most one space after the comma:If there can be multiple spaces, then I would go with the regex solution. (see icktoofay’s answer)