This site uses cookies from Google to deliver its services, to personalize ads and to analyze traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. Learn More

[rtrim] How To Remove Last Comma From The End of String in PHP?

If you want to remove the comma from the end of a string, you can use various tricks. But one simple trick is to use PHP inbuilt function rtrim(). The rtrim function (and corresponding ltrim() for left trim) is very useful as you can specify a range of characters to remove.

rtrim() strips whitespace (or other characters) from the end of a string.

Example:/>

 $string = "Hello, I, am, Anup,"; echo $string = rtrim($string, ','); //it will remove a char if the last char is a comma //output : "Hello, I, Am, Anup"; 
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); } 

source : http://liputan6.com, http://twitter.com, http://developer-paradize.blogspot.com



0 Response to "[rtrim] How To Remove Last Comma From The End of String in PHP?"

Posting Komentar

Contact

Nama

Email *

Pesan *