PHP Program to Remove Last Character from the String
Last Updated :
19 Mar, 2024
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:
Approach 1: Using substr() Function
The substr() function in PHP can be used to return a part of a string. By specifying the start and length parameters, we can effectively remove the last character. The substr() function is used with the start parameter as 0 (beginning of the string) and the length parameter as -1 (which indicates one character less from the end). This effectively returns the string without the last character.
Example: This example shows the removal of last character from the given string by the use of the substr() function.
PHP
<?php
function removeLastChars($string) {
return substr($string, 0, -1);
}
// Driver Code
$str = "Hello Geeks!";
echo "Modified String: " . removeLastChars($str);
?>
OutputModified String: Hello Geeks
Approach 2: Using rtrim() Function
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
<?php
function removeLastChars($string) {
return rtrim($string, "!");
}
// Driver Code
$str = "Hello Geeks!";
echo "Modified String: " . removeLastChars($str);
?>
OutputModified String: Hello Geeks
Approach 3: Using String Manipulation
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
<?php
function removeLastChars($string) {
return substr_replace($string, "", -1);
}
// Driver Code
$str = "Hello Geeks!";
echo "Modified String: " . removeLastChars($str);
?>
OutputModified String: Hello Geeks
Similar Reads
How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re
3 min read
Remove Last character from String in Linux In this article, we will discuss how to remove the last character from the string in Linux. In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different comman
3 min read
How to Remove Last Character from String in Ruby? Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from
2 min read
How to Remove the Last Character From a Table in SQL? SQL (Structured Query Language) allows for efficient data manipulation and retrieval. A common task in SQL involves removing the last character from a specific column within a table. This can be achieved using string functions like SUBSTRING() and LEN(). In this article, we will demonstrate how to a
4 min read
How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in
2 min read