PHP

How To Remove Last Character From String Using PHP – Examples

Some times while dealing with strings in PHP, it may be required to remove the last character of a string. In this article, I am going to share very easy ways of removing last character from string using PHP by means of multiple examples.

How To Remove Last Character Using From String Using PHP Examples

Let us assume that we have a string with some random text. So lets look at the different available ways to remove the last character from string. Consider our sample string below:

<?php

$my_string = 'Hello! This is my random string';

?>

Example 1: By using rtrim

rtrim — Strips whitespace (or other characters) from the end of a string Command:

rtrim($string,'x');

Usage:

/*Example1:=====================================================================*/
echo 'Example 1: Removing the Last character from string using PHP<br />';
echo "Before using rtrim: <br />";

//Sample String
$my_string = 'Hello! This is my random string';

//Echo the sample string
echo $my_string . '<br /><br />';

//Apply rtrim function to delete the last character from string
echo "After using rtrim: <br />";

echo rtrim($my_string, 'g');

echo '<br /><br />';

Example 2: By using substr_replace

substr_replace — Replace text within a portion of a string Command:

substr_replace($string ,"",-1);

Usage:

/*Example2:=====================================================================*/
echo 'Example 2: Removing the Last character from string using PHP<br />';
echo "Before using substr_replace: <br />";

//Sample String
$my_string = 'Hello! This is my random string';

//Echo the sample string
echo $my_string . '<br /><br />';

//Apply rtrim function to delete the last character from string
echo "After using substr_replace: <br />";

echo substr_replace($my_string ,"",-1);

echo '<br /><br />';

Example 3: By using substr

substr — Returns part of a string Command:

substr($string, 0, -1);

Usage:

/*Example3:=====================================================================*/
echo 'Example 3: Removing the Last character from string using PHP<br />';
echo "Before using substr: <br />";

//Sample String
$my_string = 'Hello! This is my random string';

//Echo the sample string
echo $my_string . '<br /><br />';

//Apply rtrim function to delete the last character from string
echo "After using substr: <br />";

echo substr($my_string, 0, -1);

echo '<br /><br />';

Example 4: By Using mb_substr

Command:

mb_substr — Get part of string

Usage:

/*Example4:=====================================================================*/
echo 'Example 4: Removing the Last character from string using PHP<br />';
echo "Before using mb_substr: <br />";

//Sample String
$my_string = 'Hello! This is my random string';

//Echo the sample string
echo $my_string . '<br /><br />';

//Apply mb_substr function to delete the last character from string
echo "After using mb_substr: <br />";

echo mb_substr($my_string, 0, -1);

echo '<br /><br />';

Your Turn!

Do you know of any other ways to use PHP to remove last character from string? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*