I'm trying to replace the last occurence of a comma in a text with "and" using strrchr()
and str_replace()
.
Example:
$likes = 'Apple, Samsung, Microsoft';
$likes = str_replace(strrchr($likes, ','), ' and ', $likes);
But this replaces the entire last word (Microsoft in this case) including the last comma in this string. How can I just remove the last comma and replace it with " and " ?
I need to solve this using strrchr()
as a function. That's why this question is no duplicate and more specific.