In this tutorial explain of php remove last character from string. step by step explain how to remove last character from string in php. I would like to show you php remove last character from string example. explain simple example of remove last character from string php.
PHP has several method to give us a ability to manipulate string such as user input and other form of data. In the below section, you will learn how to use substr. You can consider following example.
1. Using substr() function php remove last character from string
You can use the substr() function to extract of the string as specified by the offset and length parameters. If length is negative, then that character will be executed from the string end. You can start at the 0′ the position and exclude the last character from string.
Here’s how you can do it.
Syntax:
substr(string $string, int $start, ?int $length= null): string
- ‘$string’ : The input string.
- ‘$start’ : Starting position (0 based index).
- ‘$length’ : Optional, If specified, this is the length of substring. If negative, it omits that characters from the end of the string
Example
<?php
$lst_string = "Welcome to WebStuffSolution!";
/* Remove Last Character in $lst_string */
$new_lst_string = substr($lst_string, 0, -1);
echo $new_lst_string;
?>
Output:
Welcome to WebStuffSolution
2. Using substr_replace() function php remove last character from string
You can use the substr_replace() function to replace the last character of the string with an empty string. It takes the replace string, the offset position, and optionally the length of the portion.match the last character of the string and offset as -1 and skip the length parameter. substr_raplace() function main use for replace string.let’see below example.
- The original string value
- The replacement string will be added instead of the remove part.
- The mention start position.
<?php
$lst_string = "Welcome to Webstuffsolution!";
/* Remove Last character in $lst_string */
$new_lst_string = substr_replace($lst_string, '', -1);
echo $new_lst_string;
?>
In this example, removed the ‘!’ from the string. So first argument passed the original string into the callback and then provided an empty value.
Output:
Welcome to Webstuffsolution
3. Using rtrim() function php remove last character from string
This function use to remove last character from string php. You can use php function rtrim() to easily remove last character in string. let’see below example.
In this syntax, you can notice “!” character, which should be removed.
<?php
$ls_string = "Welcome to Webstuffsolution!";
/* Remove Last character in $ls_string */
$new_lst_string = rtrim($ls_string, "!");
echo $new_lst_string;
?>
In this example, rtrim($ls_string, ‘!’) removes the exclamation mark of the end of the string.
Output:
Given string: Welcome to Webstuffsolution!
Updated string: Welcome to Webstuffsolution
4. Using mb_substr() function php remove last character from string
If you’re working with multibyte strings(UTF-8 encoded string with a special character), use mb_substr() to avoid issue multibyte characters.
<?php
$ls_string = "Welcome to Webstuffsolution!";
$new_lst_string = mb_substr($ls_string, 0, -1);
echo $new_lst_string;
?>
It print the string without last character. The mb_substr function is used to get a substring that included all characters from the begining of the string (position 0) to the second-to-last character (position -1);
Welcome to Webstuffsolution
So, in this post, we discussed multiple method of removing the last character from a string in PHP. These are simple action that will help you to solve of the common issues in PHP.
I hope this tutorial help you.