How to useString Manipulation in PHP
You can also use basic string manipulation to remove the last character. The substr_replace() function is used with the start parameter as -1 and the replacement string as an empty string. This effectively replaces the last character with an empty string, removing it.
Example: This example shows the removal of last character from the given string by using string manipulation.
<?php
function removeLastChars($string) {
return substr_replace($string, "", -1);
}
// 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