从字符串[重复]中删除最后一个字符

Possible Duplicate:
PHP - Remove last character if it's a period?

Which is fastest way to remove last character from string?

I have a string like

a,b,c,d,e,

I would like to remove last ',' and get the remaining string back

OUTPUT: a,b,c,d,e

What the fastest way to do this?

转载于:https://stackoverflow.com/questions/5592994/remove-the-last-character-from-string

First I try without space rtrim($arraynama,","); and get error result.

Then I add a space and get good result: $newarraynama=rtrim($arraynama,", ");

You can use substr

echo substr('a,b,c,d,e,', 0, -1);
# => 'a,b,c,d,e'

You can use

substr(string $string, int $start, int[optional] $length=null);

See substr in the PHP docs. It returns part of a string.

An alternative to substr is the following, as a function:

substr_replace($string, "", -1)

Is it the fasted? I don't know, but I'm willing to bet these alternatives are all so fast that it just doesn't matter.