Date: 03/08/06 (PHP Community) Keywords: no keywords I'm trying to figure out the most efficient way to ensure that a space follows a comma in a string. For example I have an input string: "C1,C2,C3"with no spaces after the commas. I'd like to convert that to "C1, C2, C3".I can think of several ways of doing this iterating over each character in a string checking for commas not followed by spaces, or exploding the string, trimming whitespace, adding a space and concatenating it all back together again. But it seems like a preg_replace() or a ereg_replace() might be a more efficient solution. I don't want to add a space if it already exists. I have tried this: ereg_replace(',[^ ]',', ','C1,C2,C3');but it gives me: "C1, 2, 3"Any suggestions? I'm not a regular expressions expert at all.
|