HOW TO: REMOVE TRAILING COMMAS IN PHP

During conversion of an array of information into a string using PHP we often face this problem.
Suppose, we are fetching data from a database and building a comma separated string value within
a loop. Then the loop will add an extra comma at the end of such string.

To remove the trailing comma use following PHP function:

1. function RemoveTrailingCommas($your_string) {
2. return trim(preg_replace(“/(.*?)((,|s)*)$/m”, “$1”, $your_string));
3. }

One thought on “HOW TO: REMOVE TRAILING COMMAS IN PHP

  1. Hello, i think that i saw you visited my weblog thus i got here to return the desire?

    .I am trying to find issues to improve my web site!I assume its
    good enough to make use of a few of your
    concepts!!

Leave a comment