How to Remove the Last Character from a String in PHP
Topic: PHP / MySQLPrev|Next
Answer: Use the rtrim()
Function
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:
Example
Try this code »<?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
?>
Related FAQ
Here are some more FAQ related to this topic: