PHP remove last character from string
In this article, you will learn how to remove the last character from a string using the PHP programming language.
This is a very common PHP interview question and is generally asked in programming interviews, 'how to remove the last character from a string in PHP'. Or, during the development process, you may come across a situation where you need to eliminate the last character from a string. This exercise shows you different ways to achieve the process from the given string. You can use any of the following methods to remove the last character from the string.
PHP remove last character from string using substr() function
This function is used to return a part of a string or substring from a string, starting from a given position.
Syntax-substr(string,start,length)
The string is the required parameter. It should be a string datatype, start specifies where to start in the string and length is an optional parameter. If it is positive, the returned string contains the length of characters from the beginning, and if it is negative, the returned string contains the length of characters from the end of the string.
Example -<?php
$str1 = "Good Morning!";
echo "Original string: " . $str1 . "<br/>";
echo "Updated string: " . substr($str1, 0, -1) . "<br/>";
?>
Output -
Original string: Good Morning!
Updated string: Good Morning
PHP remove last character from string using mb_substr() function
It returns the part of the string. It performs a multi-byte safe substr() operation based on the number of characters. The position is counted from the beginning of the string.
Syntax -mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] )
Here, $str is the string to extract the substring from, $start specifies where to start in the string, and $length specifies the maximum number of characters to use from $str.
Example -<?php
$str1 = "Good Morning!";
echo "Original string: " . $str1 . "<br/>";
echo "Updated string: " . mb_substr($str1, 0, -1) . "<br/>";
?>
Output -
Original string: Good Morning!
Updated string: Good Morning
PHP remove last character from string using substr_replace() function
It is used to replace a part of a string with another string.
Syntax -substr_replace($string,$replacement,$start,$length)
Here, $string is the string to extract, $replacement specifies the string to insert, $start specifies where to start replacing in the string, and $length specifies how many characters should be replaced.
Example -<?php
$str1 = "Good Afternoon";
echo "Original string: " . $str1 . "<br/>";
echo "Updated string: " . substr_replace($str1, "", -1) . "<br/>";
?>
Output -
Original string: Good Afternoon
Updated string: Good Afternoo
PHP remove last character from string using rtrim function
It is used to remove whitespace and other predefined characters from the right side of a string.
Syntax -rtrim($string,$charlist);
Here, $string is the string to extract, and $charlist specifies which character to remove from the string.
Example -<?php
$str1 = "Have a nice day!";
echo "Original string: " . $str1 . "<br/>";
echo "Updated string: " . rtrim($str1, "!") . "<br/>";
?>
Output -
Original string: Have a nice day!
Updated string: Have a nice day
Related Articles
How to pass array in URL query string using PHPPHP remove last character from string
Remove duplicates from array PHP
PHP capitalize first letter
PHP calculate percentage of total
PHP String Contains
Fibonacci number program
PHP reverse a string without predefined function
PHP random quote generator
PHP substr() function
PHP convert string into an array
PHP remove HTML and PHP tags from string
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
PHP 7 Scalar Type Declaration
PHP ob_get_contents() function
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP
PHP strlen() function