How to String Replace Last Character in PHP?

03-Apr-2023

.

Admin

How to String Replace Last Character in PHP?

Hi Dev,

If you need to see example of how to string replace last character in php?. I explained simply about how do i remove the last character of a string?. I explained simply step by step how do you change the last element of a string?. I would like to show you how can i remove one character from a string in php?.

There are example to php check if string replace last character in php. in this example, we will use to rtrim() function to check if string replace last character in php . so let's the following example with output.

Example 1:


index.php

<?php

// Sample string

$str1 = "Hello World!";

echo rtrim($str1, "!");

// Sample string

$str2 = "red, green, blue,";

echo rtrim($str2, ",");

?>

Output:

Hello World

red, green, blue

I hope it could help you...

#PHP