PHP Functions: Remove Last Character From String In PHP
In this tutorial, you’ll learn four methods to remove the last character from a string in PHP. You can use any one of the following methods as per the requirements.
I’m assuming that you have basic knowledge of PHP language and have it running on your computer.
Contents
substr_replace function
The substr_replace() function replaces a part of a string with another string.
Syntax
substr_replace(string,replacement,start,length)
Description
- string required value and specifies the string to check
- replacement required value and specifies the string to insert
- start expected value and determines where to begin replacing in the string
- A positive number – Start replacing at the specified position in the string
- Negative number – Start replacing at the specified position from the end of the string
- 0 – Start replacing at the first character in the string
Usage
Using substr_replace() to remove the last haracter from a String.
<?php
$string = 'Codesource.';
echo substr_replace($string, "", -1);
Output
Codesource
substr function
The substr() function returns a part of a string.
Syntax
substr(string,start,length)
Description
- string is a required value and Specifies the string to return a part of
- start is a required value and specifies where to start in the string
- A positive number – Start at a specified position in the string
- A negative number – Start at a specified position from the end of the string
- 0 – Start at the first character in the string
- length is an optional value and specifies the length of the returned string. The default is to the end of the string.
- A positive number – The length to be returned from the start parameter
- Negative number – The length to be returned from the end of the string
- If the length parameter is 0, NULL, or FALSE it returns an empty string
Usage
Using substr() to remove the last haracter from a String.
<?php
$string = 'Codesource.';
echo substr($string, 0, -1);
Output
Codesource
rtrim
The trim() function removes whitespace and other predefined characters from both sides of a string. There are two variations of this function:
- ltrim() removes whitespace or other predefined characters from the left side of a string
- rtrim() removes whitespace or other predefined characters from the right side of a string
Syntax
trim(string,charlist)
Description
- string is a required value and specifies the string to check.
- charList is an optional value ans specifies which characters to remove from the string.
Usage
In this tutorial we’ll cover the rtrim() function. Let’s see how to remove the last character using rtrim().
<?php$string = 'Codesource.';echo rtrim($string, '.'). "n";
Output
Codesource
mb_subtr function
Use the mb_subtr() function to remove characters from the end of the string.
Syntax and description
mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] )
- $str The string to extract the substring from.
- $start
- A positive number – the returned string will start at the start‘th position in str, couting from zero.
- A negative number – the returned string will start at the start‘th character from the end of str.
Usage
<?php
$string = 'Codesource.';
echo mb_substr($string, 0, -1). "n";
Output
Codesource
Conclusion
Congratulations! Now you can remove or replace any character from a String.
Thanks for reading.