Error in your code:
$categories = "<category>" . $cat . "</category>\n";
You rewrite $categoriesat each iteration, it should be:
$categories .= "<category>" . $cat . "</category>\n";
Not sure if I will be back on this?
Find and replace is not for what an explosion is needed. If you just want to fix a code error - see above.
It is more efficient:
$categories = "<category>" .
str_replace(', ', "</category>\n<category>", $input) .
"</category>\n";
And this also takes into account variable spaces:
$categories = "<category>" .
preg_replace('@\s*,\s*@', "</category>\n<category>", $input) .
"</category>\n";
source
share