Skip to content

How to Remove Last Character from String in PHP

Use PHP substr_replace() function to remove last charecter from string. How do I remove last character from a string in PHP script?

Category
PHP
On this page

Question – How do I remove last character from a string in PHP script? In this tutorial you will learned multiple methods to remove last character from a string using php.

This tutorial describes 4 methods to remove last character from a string in PHP programming language. You can use any one of the following methods as per the requirements.

Method 1 – Using substr_replace function

You can use the PHP substr_replace() function to remove the last character from a string in PHP.

Syntax:

Example:

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

Method 2 – substr function

Use the substr function to remove the last character from any string in PHP string

Syntax:

Example:

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

Method 3 – mb_substr function

Use the mb_substr function to remove characters from the end of the string.

Syntax:

Example:

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

Method 4 – rtrim function

The rtrim function to used to remove specific characters from the end of the string.

Syntax:

Here “x” is the character to remove.

Example:

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

6 Comments

  1. Your article has piqued my interest. This is definitely a thinker’s article with great content and interesting viewpoints. I agree in part with a lot of this content. Thank you for sharing this informational material.

  2. Your article has piqued my interest. This is definitely a thinker’s article with great content and interesting viewpoints. I agree in part with a lot of this content. Thank you for sharing this informational material.

      1. Hi Ben, I don’t think we have time to do fake comments. We are getting hundreds of spam comments daily, so some of them with positive messages get approved by the team.

Leave a Reply

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


Back to top