132

I want to remove the comma off the end of a string. As it is now I am using

$string = substr($string,0,-1);

but that only removes the last character of the string. I am adding the string dynamically, so sometimes there is no comma at the end of the string. How can I have PHP remove the comma off the end of the string if there is one at the end of it?

peterh
  • 11,875
  • 18
  • 85
  • 108
zeckdude
  • 15,877
  • 43
  • 139
  • 187

10 Answers10

346
$string = rtrim($string, ',');

Docs for rtrim here

Manse
  • 37,765
  • 10
  • 83
  • 108
Sigurd
  • 7,865
  • 3
  • 24
  • 34
  • 30
    This will remove multiple commas: "a,b,,," will become "a,b". Whether that's what the OP wants or not I don't know... – Greg Oct 29 '09 at 10:20
  • 1
    Thank you, I was doing this with substr, strlen,... all my life – Farhad Apr 15 '15 at 17:59
48

This is a classic question, with two solutions. If you want to remove exactly one comma, which may or may not be there, use:

if (substr($string, -1, 1) == ',')
{
  $string = substr($string, 0, -1);
}

If you want to remove all commas from the end of a line use the simpler:

$string = rtrim($string, ',');

The rtrim function (and corresponding ltrim for left trim) is very useful as you can specify a range of characters to remove, i.e. to remove commas and trailing whitespace you would write:

$string = rtrim($string, ", \t\n");
Ben Russell
  • 1,413
  • 12
  • 15
15

i guess you're concatenating something in the loop, like

foreach($a as $b)
  $string .= $b . ',';

much better is to collect items in an array and then join it with a delimiter you need

foreach($a as $b)
  $result[] = $b;

$result = implode(',', $result);

this solves trailing and double delimiter problems that usually occur with concatenation

user187291
  • 53,363
  • 19
  • 95
  • 127
  • Good suggestion. However, not all concatenations may take place in PHP. In my case, I am sending a Javascript concatenated string for PHP to unravel. – Sablefoste Sep 11 '15 at 19:49
  • This is beautiful and now I have to rewrite everything. Can't believe I overlooked such a simple and straightforward answer – Ryan Deckard Sep 08 '20 at 23:06
  • @Sablefoste ...then don't send a mess to unravel. Javascript has `join()` as well. – mickmackusa Jun 15 '22 at 04:01
9

If you're concatenating something in the loop, you can do it in this way too:

$coma = "";
foreach($a as $b){
    $string .= $coma.$b;
    $coma = ",";
}
cesar.mi
  • 111
  • 1
  • 4
3

have a look at the rtrim function

rtrim ($string , ",");

the above line will remove a char if the last char is a comma

Anand Shah
  • 14,575
  • 16
  • 72
  • 110
1
if(substr($str, -1, 1) == ',') {

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

}

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

TigerTiger
  • 10,590
  • 15
  • 57
  • 72
1

A simple regular expression would work

$string = preg_replace("/,$/", "", $string)
AlexWilson
  • 1,179
  • 1
  • 9
  • 16
1

rtrim ($string , ","); is the easiest way.

GayanM
  • 152
  • 1
  • 1
  • 9
1

I had a pesky "invisible" space at the end of my string and had to do this

 $update_sql=rtrim(trim($update_sql),',');

But a solution above is better

 $update_sql=rtrim($update_sql,', ');
zzapper
  • 4,743
  • 5
  • 48
  • 45
0

Precede that with:

if(substr($string, -1)==",")
Kaivosukeltaja
  • 15,541
  • 4
  • 40
  • 70