Cut off last character in string PHP

You are coding in PHP and you have a string a,b,c,d,. You want to get rid of the last comma (last character) from this string to get just a,b,c,d. This short tutorial will show you a few ways to do it:

Cut off last character from string using substr
$string = "a,b,c,d,";
$string = substr($string, 0, -1);
Cut off last 2 characters from string using substr
$string = "a, b, c, d, ";
$string = substr($string, 0, -2);
Cut off last character from string using rtrim. For this method, you must know what the last character of the string is.
$string = "a,b,c,d,";
$string = rtrim($string, ",");
Cut off last 2 characters from string using rtrim. For this method, you must know what the last 2 characters of the string are.
$string = "a, b, c, d, ";
$string = rtrim($string, ", ");
An Do By An Do
12/09/2016

Copyright © Break Project 2014