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.


Use the rtrim function:

rtrim($my_string, ',');

The Second parameter indicates the character to be deleted.


Try:

$string = "'name', 'name2', 'name3',";
$string = rtrim($string,',');

Try the below code:

$my_string = "'name', 'name2', 'name3',";
echo substr(trim($my_string), 0, -1);

Use this code to remove the last character of the string.