PHP: Remove One or Two Last Characters from a String

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!

Divine
Author: Divine

Official account of the System Administrator.

22 thoughts on “PHP: Remove One or Two Last Characters from a String

  1. Very quickly this website will be famous among all blogging people, due to it’s fastidious articles or reviews.

    1. Thanks Bennett,

      We are working hard to ensure that we bring such minds together and deliver great products and opportunities to the world. Check back often and find out what come up.

      Divine.

  2. Magnificent web site. Lots of useful info here.
    I am sending it to a few friends ans also sharing in delicious.
    And certainly, thank you to your sweat!

    1. Hi Celesta,

      We are happy to note that this article could make a change for you. Please advise on other areas you would like to have contents improved in.

      Regards,
      Divine

    1. You are welcome.

      We would like to know, is there any particular section that resolved your specific problem? Let us know so we can continue to improve such content online.

      Regards,
      Divine

  3. You article definitely has all the information and facts I needed about this subject. I didn’t know who to ask, so thank you kindly…

    1. You are welcome Supreme Search. We are happy we could assist in our little way towards your getting what you needed. We continue to inspire and promote contents like this so that our Tech Community is ever improved.

      Regards,
      Divine

  4. Hola This blog was… how do I say it? Relevant!! Finally I’ve found something that helped me. thank you

    1. Hi,

      I’m glad you got the necessary help from it. We continue to do even more to help our people in the programming community so that IT can take central stage in the communication world. If you have other areas you might be interested in taking up lessons in, do let us know.

      Regards,
      Divine

  5. Hello ,Thankfulness to my father who shared with me on the topic of this webpage, this website is actually awesome. danke

    1. I’m happy we could help in this way. Continue to share as the need arises. Let us together meet as many people as possible.

      Regards,
      Divine

Leave a Reply

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