I have a comma delimited list I want to import into a database, and in some cases the last item is blank:
item1, item2, item3
item1, item2,
item1, item2,
I’d like to replace all of these empty columns with a placeholder value using a regexp
item1, item2, item3
item1, item2, PLACEHOLDER
item1, item2, PLACEHOLDER
I tried this:
preg_replace("/,\n/", ",PLACEHOLDER\n",$csv);
…but it isn’t working. Anyone know what regexp would work for this?
preg_replace("/,\s*$/m", ", PLACEHOLDER\n", $csv);should do it. This pattern matches a comma, followed by any number of spaces, followed by the end of the line, and replaces it with your placeholder text.