PHP: Remove Last Character from String – substr/substr_replace/rtrim

This is a very common PHP question, how to remove last character from string in PHP?

So here are three ways how to delete last character from string in PHP.

Method 1 – PHP: Remove Last Character from String using substr and mb_substr

substr and mb_substr commands usage

substr and mb_substr example:

Example output:

Method 2 – PHP: Remove Last Character from String using substr_replace

substr_replace command usage

substr_replace example:

Example output:

Method 3 – PHP: Remove Last Character from String using rtrim

Note: rtrim function is not working exactly same way as substr and substr_replace, but it is of course useful some cases. Rtrim function trims all specified characters from end of the string.

rtrim command usage

rtrim example:

Example output:

Share this post


  1. so what is the fastest and most efficient method of methods mentioned above?

  2. Thanks! Very useful (: I was using “slice” from JS -> mystring.slice(0,-1) to remove last comma in a string result from php but now I know that with substr($mystring, 0, -1) I get the same result.

  3. Thanks great work i was searching for it.

  4. thanks it help me much

  5. thanks nice great job.

  6. Thanks for the a basic but a useful post. Was not sure about all 3 options.
    Will follow few more posts from you.


Submit a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">