PHP: Replace One or Two Last Characters from a String using: substr/substr_replace/rtrim

PHP: Remove One or Two Last Characters from a StringCommon PHP, entry programmers most often as the question, how to remove one or two last characters from a string in PHP?

Three ways have been identified on how to delete last more than one last characters from a string in PHP.

Method 1 – PHP: Remove One or Two Last Characters from a String using substr and mb_substr

substr and mb_substr commands usage

substr($string, 0, -1);
mb_substr($string, 0, -1);

substr and mb_substr example:

$string = "This is test string..";
echo $string . "<br>";

// substr function
echo "substr: " . substr($string, 0, -1);

echo "<br>";

// mb_substr multibyte version
echo "mb_substr: " . mb_substr($string, 0, -1);

echo "<br>";

Example output:

This is test string..
This is test string.
This is test string.

You may also remove the two last characters as follows;

$string = "This is test string...";
echo $string . "<br>";

// substr function
echo "substr: " . substr($string, 0, -2);

echo "<br>";

// mb_substr multibyte version
echo "mb_substr: " . mb_substr($string, 0, -2);

echo "<br>";

Example output:

This is test string...
This is test string.
This is test string.

 

Method 2 – PHP: Remove One or Two Last Characters from a String using substr_replace

substr_replace command usage

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

substr_replace example:

$string = "This is test string..";
echo $string . "<br>";

// substr_replace function
echo "substr_replace: " . substr_replace($string ,"",-1);

echo "<br>";

Example output:

This is test string..
This is test string.

You may also remove the two last characters as follows;

$string = "This is test string...";
echo $string . "<br>";

// substr_replace function
echo "substr_replace: " . substr_replace($string ,"",-2);

echo "<br>";

Example output:

This is test string...
This is test string.

 

Method 3 – PHP: Remove a set of Closing Characters from a String using rtrim

Note: rtrim function does not work exactly the same as substr and substr_replace, however it is of course useful in some cases. rtrim function trims all specified characters from end of the string.

rtrim command usage

rtrim($string,'x');

rtrim example:

$string = "This is test string..";
echo $string . "<br>";

// rtrim function
echo "rtrim: " . rtrim($string, ".");

echo "<br>";

Example output:

This is test string..
This is test string

You may also remove the last ‘s’ characters as follows;

$string = "He is a boss";
echo $string . "<br>";

// rtrim function
echo "rtrim: " . rtrim($string, "s");

echo "<br>";

Example output:

He is a boss
He is a bo

 

Further Reading

Thank you for visiting our page, and for taking the time to read through this document. It is important to note that as a programmer, learning never ends and it is our calculated effort to continuously serve you with valid contents to boost your experience in your desired field. With additional research, we hope to dig deeper and provide other ways by which you can remove last characters from a string, as well as how to integrate that knowledge into a security based system for better productivity.

This post first appeared in If Not True Then False. Comment or add your ideas below. You may also visit our services to see how we can serve you better. Let us know what you think!

Admin
Author: Admin

Official account of the System Administrator.

Leave a Reply

Your email address will not be published. Required fields are marked *