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.
Using the substring() method We remove the last comma of a string by using the built-in substring() method with first argument 0 and second argument string. length()-1 in Java. Slicing starts from index 0 and ends before last index that is string. length()-1 .
You can use the PHP rtrim() function to remove the last character from the given string in PHP.
To remove the last three characters from a string, we can use the substr() function by passing the start and length as arguments.
Using trim : trim($dataList, '*'); This will remove all * characters (even if there are more than one!) from the end and the beginning of the string.
Use the rtrim
function:
rtrim($my_string, ',');
The Second parameter indicates the character to be deleted.
Try:
$string = "'name', 'name2', 'name3',"; $string = rtrim($string,',');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With