How to remove last comma in string using php

To remove/trim the last/trailing or first/heading comma from a string, one should first confirm that the last character of the string is comma itself and nothing else. This would make the code foolproof and prevent accidental removal of characters other than comma.

$string =  "'apple', 'car', 'ball',";
$output = rtrim($string,',');

Leave a Reply

Your email address will not be published. Required fields are marked *