Remove the last character from string

  • Added:
  • |
  • In: Basic PHP

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


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?

This Question Has 4 Answeres | Orginal Question | I-M-JM

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

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

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

You can use substr("a,b,c,d,e,", 0, -1);

The output is a,b,c,d,e

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.

Meet with owner

Sajjad Hossain

Hey, I am Sajjad, working in web development sector since 2012. I love to do amazing things. Let's do a project together.
Connect Social With PHPAns