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

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?

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.


Search
I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.

Connect Social With PHPAns
Top