Q:

How to Remove the Last Character from a String in PHP

0

How to Remove the Last Character from a String in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Return JSON from a PHP Script... >>
<< How to Refresh a Page Periodically Using PHP...