$xcatname = 'Business';
$repeatmaincitiescat = 'London, Paris, New York';
$repeatmaincitiescat = str_replace(',', ' ' . $xcatname . ',', $repeatmaincitiescat).(empty($repeatmaincitiessubcat)?'':"");
Above codes giving this result: London Business, Paris Business, New York Business
However, I want this type of output,
Business in London, Business in Paris, Business in New York
I tried a few changes but it turns out to be in error.
An alternative to what hakre proposed is to use regular expressions. It’s as close as you can get to your code, what you want to achieve cannot be done with a simple replace.
So, you could do (to be as close as possible to what you did):