PHP: How to rewmove last comma from a string

// November 25th, 2008 // PHP Tips

Hello readers

A few minutes ago, I had a situation where

For example you have a string which is like “cat,cow,hamster,owl,”

and you want to remove the last comma from the string

then you can use this code

<?php
$string="cat,cow,hamster,owl,";
echo $string."<br/>";
$string= substr($string, 0, strlen($string)-1);
echo $string;
?>

This comes really handy in many situations.

Leave a Reply