[www@henrik ~]$ blog -12

Remove last character from string in PHP

2009-02-24, from the Tumblr years

After looking around for a way to actually remove the last character of my string even if it was not the last one in the string given, I came up with these two ways. I figure that the one using strrev() is a lot faster then the one using explode(), but as someone might want the first way, I publish it anyway.

This was the way I had done to remove the last character of any given string:

$str = substr($str,0,strlen($str)-1);

This was the second thing I tried (SLOWER)

$string = "adml,madadsdsadsadasdsa,    asd";
$string = explode(",", $string);
array_pop($string);
$string = implode(",", $string);
// Will output: adml,madadsdsadsadasdsa
echo $string;

This is probably one of the better way of doing it! (FASTER)

$string = "adml,madadsdsadsadasdsa,    asd";
$string = strrev($string);
$string substr($string,strpos($string,",")+1);
// Will output: adml,madadsdsadsadasdsa
echo strrev($string);

Type "ls -a" for the list of all posts.