How to Remove the Last Character from a String in PHP
rtrim()
You can simply use the rtrim() function to remove the last character from a string.
Let's take a look at an example to understand how it actually works:
<?php // Sample string $str1 = "Hello World!"; echo rtrim($str1, "!"); // Outputs: Hello World echo "<br>"; // Sample string $str2 = "red, green, blue, "; echo rtrim($str2, ", "); // Outputs: red, green, blue ?>
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Use the
rtrim()FunctionYou can simply use the
rtrim()function to remove the last character from a string.Let's take a look at an example to understand how it actually works:
need an explanation for this answer? contact us directly to get an explanation for this answer