WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

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:

<?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:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties