-1

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

Another string like

a,b,,,

OUTPUT

a,b
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
Sambhu M R
  • 297
  • 5
  • 23
  • what have tried so far ? – Halayem Anis Mar 01 '16 at 09:10
  • Possible duplicate of [PHP - How to remove all specific characters at the end of a string?](http://stackoverflow.com/questions/2053830/php-how-to-remove-all-specific-characters-at-the-end-of-a-string) – Pavel V. Mar 01 '16 at 09:31

4 Answers4

1

If you just want to remove the , on the right side, you could use rtrim, if you need also the left side, trim is your choice. If there are also some , inside, you could split and merge the string again.

$data = join(',', array_filter(explode(',', $data)));
Philipp
  • 15,377
  • 4
  • 35
  • 52
0

Like this:

trim("a,b,,,", ",");

http://php.net/manual/en/function.trim.php

markdwhite
  • 2,360
  • 19
  • 24
0

Check out this:

echo rtrim('a,b,,,', ',');

Read Manual for more details.

Fakhruddin Ujjainwala
  • 2,493
  • 17
  • 26
0

The rtrim() function removes whitespace or other predefined characters from the right side of a string.

For more details rtrim PHP

rtrim("a,b,,,", ",");
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51