How do I remove a comma off the end of a string?

Php

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Docs for rtrim here

Solution 2 - Php

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");

Solution 3 - Php

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

Solution 4 - Php

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 = ",";
}

Solution 5 - Php

have a look at the rtrim function

rtrim ($string , ",");

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

Solution 6 - Php

if(substr($str, -1, 1) == ',') {

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

}

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

Solution 7 - Php

A simple regular expression would work

$string = preg_replace("/,$/", "", $string)

Solution 8 - Php

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

Solution 9 - Php

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,', ');

Solution 10 - Php

Precede that with:

if(substr($string, -1)==",")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzeckdudeView Question on Stackoverflow
Solution 1 - PhpSigurdView Answer on Stackoverflow
Solution 2 - PhpBen RussellView Answer on Stackoverflow
Solution 3 - Phpuser187291View Answer on Stackoverflow
Solution 4 - Phpcesar.miView Answer on Stackoverflow
Solution 5 - PhpAnand ShahView Answer on Stackoverflow
Solution 6 - PhpTigerTigerView Answer on Stackoverflow
Solution 7 - PhpAlexWilsonView Answer on Stackoverflow
Solution 8 - PhpmepoView Answer on Stackoverflow
Solution 9 - PhpzzapperView Answer on Stackoverflow
Solution 10 - PhpKaivosukeltajaView Answer on Stackoverflow