To remove the last character from a string in php, the easiest way is with php substr() function.

$string = "This is a string variable";

$string_without_last_char = substr($string,0,-1);

echo $string_without_last_char;

// Output:
This is a string variabl

You can also use the php rtrim() function.

$string = "This is a string variable";

$string_without_last_char = rtrim($string,$string[-1]);

echo $string_without_last_char;

// Output:
This is a string variabl

When using string variables in php, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable.

For example, we can easily remove the last character from a string in php. To remove the last character from a string in php, we can use the substring function substr().

To remove the last character in a string using substr(), we know that the last character has index ‘-1’. So, to get everything except the last character, we want to get everything up until the last position, or the ‘-1’ position.

Below is an example of how to get rid of the last character in a string using php.

$string = "This is a string variable";

$string_without_last_char = substr($string,0,-1);

echo $string_without_last_char;

// Output:
This is a string variabl

You can also use substr() to remove the first character from a string in php in a very similar way. To remove the first character in a string, pass ‘1’ as the start position.

$string = "This is a string variable";

$string_without_first_char = substr($string, 1);

echo $string_without_first_char;

// Output:
his is a string variable

Using the php rtrim() Function to Remove Last Character from String

In php, the string method rtrim() removes the characters from the right side of the string that is given to it.

We can use the rtrim() method to remove the last character of a string by passing the last character of the string to the function.

Below is how you can use the rtrim() function in php to get rid of the last character in a string.

$string = "This is a string variable";

$string_without_last_char = rtrim($string,$string[-1]);

echo $string_without_last_char;

// Output:
This is a string variabl

How to Remove the Last N Characters from String Using php

We can also use the substr() function to get rid of the last n characters of a string easily in php.

To remove the last n characters from a string using php, you just need to change the input to the end position argument of the slice you want.

For example, to remove the last 5 characters from a string, you pass ‘-5’ as shown in the following php code.

$string = "This is a string variable";

$string_without_last_5_char = substr($string,0,-5);

echo string_without_last_5_char;

// Output:
This is a string var

How to Remove the Last and Last Character from a String Using php

Another example of how we can use substr() to remove characters from a string in php is the removal of the first and last character from a string.

With substr()again, we can easily get rid of the first and last character from a string.

To do so, we need to select all of the elements between the first and last character.

Below is how we can remove the first and last character from a string in php.

$string = "This is a string variable";

$string_without_first_or_last_char = substr($string,1,-1);

echo $string_without_first_or_last_char;

// Output:
his is a string variabl

How to Remove a Character at a Specific Position in a String Using php

The last example I want to share with you in this article is how to remove a character from a string at a specific position.

Let’s say we want to remove the sixth character from a string.

To do so, we can create two substrings from our original string and then concatenate them. We first create a string from the start of our string to the desired position, and then create another string from the desired position + 1 to the end.

Below is an example of how to use substr() in php to remove a character at a specific position.

$string = "This is a string variable";

$string_without_sixth_char = substr($string,0,6) . substr($string,6+1);

echo $string_without_sixth_char;

// Output:
This i a string variable

Hopefully this article has been beneficial for you to learn how to remove the last character from a string in your php code.

Categorized in:

PHP,

Last Update: February 26, 2024