How do I remove the last comma from a string using PHP?

JoJo picture JoJo · Mar 14, 2013 · Viewed 193.2k times · Source

I am using a loop to get values from my database and my result is like:

'name', 'name2', 'name3',

And I want it like this:

'name', 'name2', 'name3'

I want to remove the comma after the last value of the loop.

Answer

Ander2 picture Ander2 · Mar 14, 2013

Use the rtrim function:

rtrim($my_string, ',');

The Second parameter indicates the character to be deleted.