phpstringsubstr

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


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.


Solution

  • You may use the rtrim function. The following code will remove all trailing commas:

    rtrim($my_string, ',');
    

    The Second parameter indicates characters to be deleted.