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.
just provide the answer with function
strrchr()
because
strrchr()
willSee Doc here
so we just need only replace the comma symbol should be fine. and the comma will be always the first character when you use
strrchr()
See Demo here