How to usertrim() Function in PHP
The rtrim() function is another way to remove the last character from a string, especially if the character is a known whitespace or other specific character. The rtrim() function is used with the character to be trimmed specified as the second argument. This approach is useful when you specifically want to remove a known character from the end of the string.
Example: This example shows the removal of last character from the given string by the use of the rtrim() function.
<?php
function removeLastChars($string) {
return rtrim($string, "!");
}
// Driver Code
$str = "Hello Geeks!";
echo "Modified String: " . removeLastChars($str);
?>
Output
Modified String: Hello Geeks
PHP Program to Remove Last Character from the String
Given a String, the task is to remove the last character from a string in PHP. This operation is common in text processing and manipulation.
Examples:
Input: str = "Hello Geeks!"
Output: Hello Geeks
Input: str = "Welcome"
Output: Welcom
These are the following approaches:
Table of Content
- Using substr() Function
- Using rtrim() Function
- Using String Manipulation