Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php how to remove any last commas

Tags:

php

trim

Example output

1. test,test,test,test,test, 2. test,test,test,, 3. test,test,,, 4. test,,,,, 

I tried use implode according to my previous question but It's trim only last comma.

How to remove any last commas?

like image 970
wow Avatar asked Jun 25 '10 18:06

wow


People also ask

How do I remove the last comma from a string?

To remove the last comma from a string, call the replace() method with the following regular expression /,*$/ as the first parameter and an empty string as the second. The replace method will return a new string with the last comma removed. Copied!

How can I remove last 5 characters from a string in PHP?

You can use the PHP substr_replace() function to remove the last character from a string in PHP. $string = "Hello TecAdmin!"; echo "Original string: " . $string .


1 Answers

rtrim('test,,,,,', ','); 

See the manual.

like image 88
Artefacto Avatar answered Sep 24 '22 04:09

Artefacto