Monday, April 15, 2013

PHP functions (substr,rtrim) to remove last character from a string


<?php
$string="apple,ball,cat,";
echo substr($string, 0, -1);
echo "<br/>";
echo rtrim($string, ",");
?>


Output:
apple,ball,cat
apple,ball,cat

rtrim is not the same as substr. Difference is shown below


<?php
$string="apple,ball,cat,,";
echo rtrim($string, ",");
?>


Output:
apple,ball,cat

As seen from the above example rtrim trims all specified character from the end part of the string.

No comments:

Post a Comment