PHP remove last character from string
In this article, you will learn how to remove last character from a string using PHP.
This is a very common PHP question and generally asked in an interview, 'how to remove last character from string in PHP?'. Or, while working with PHP, you may need to eliminate characters from strings. This exercise shows you different ways for removing the last character from the given string. You can use any of the following methods to remove the last character from the string.
Method 1 : 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 string datatype, start specifies where to start in the string and length is optional parameter. If it is positive, the returned string contains length characters from the beginning and if it is negative, the returned string contains length characters from the end of the string.
Example -<?php
$str1 = "Good Morning!";
echo "Original string: " . $str1 . "\n";
echo "Updated string: " . substr($str1, 0, -1) . "\n";
?>
Method 2 : mb_substr() function
It returns the part of the string. It performs a multi-byte safe substr() operation based on number of characters. The position is counted from the beginning of string.
Syntax -mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] )
Here, the $str is the string to extract the substring from, $start specifies where to start in the string, $length specifies the maximum number of characters to use from $str.
Example -<?php
$str1 = "Good Morning!";
echo "Original string: " . $str1 . "\n";
echo "Updated string: " . mb_substr($str1, 0, -1) . "\n";
?>
Method 3 : 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, $length specifies many characters should be replaced.
Example -<?php
$str1 = "Good Morning!";
echo "Original string: " . $str1 . "\n";
echo "Updated string: " . substr_replace($str1, "", -1) . "\n";
?>
Method 4 : rtrim function
It is used to remove the whitespaces 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 = "Good Morning!";
echo "Original string: " . $str1 . "\n";
echo "Updated string: " . rtrim($str1, "!") . "\n";
?>
Related Articles
PHP array lengthImport Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP random quote generator
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP