Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»PHP»How to Remove Last Character from String in PHP

    How to Remove Last Character from String in PHP

    By RahulOctober 8, 20202 Mins Read

    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.

    Advertisement

    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.

    • Don’t Miss – Check If String Contains a Sub String in PHP

    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:

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

    Example:

    1
    2
    3
    4
    5
    6
    <?Php
    $string = "Hello TecAdmin!";
    echo "Original string: " . $string . "\n";
     
    echo "Updated string: " . substr_replace($string ,"",-1) . "\n";
    ?>

    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:

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

    Example:

    1
    2
    3
    4
    5
    6
    <?php
    $string = "Hello TecAdmin!";
    echo "Original string: " . $string . "\n";
     
    echo "Updated string: " . substr($string, 0, -1) . "\n";
    ?>

    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:

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

    Example:

    1
    2
    3
    4
    5
    6
    <?php
    $string = "Hello TecAdmin!";
    echo "Original string: " . $string . "\n";
     
    echo "Updated string: " . mb_substr($string, 0, -1) . "\n";
    ?>

    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:

    1
    rtrim($string,'x');

    Here “x” is the character to remove.

    Example:

    1
    2
    3
    4
    5
    6
    <?php
    $string = "Hello TecAdmin!";
    echo "Original string: " . $string . "\n";
     
    echo "Updated string: " . rtrim($string, "!") . "\n";
    ?>

    Output:

    Original string: Hello TecAdmin!
    Updated string: Hello TecAdmin
    

    PHP substr substring
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    PHP Arrays: A Beginner’s Guide

    Running Laravel Queue Worker as a Systemd Service

    How to Change PHP Session Timeout

    View 6 Comments

    6 Comments

    1. atcodex on October 8, 2020 2:20 am

      very good article

      Reply
    2. Robert Ross on September 5, 2019 7:53 am

      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.

      Reply
    3. simon on March 9, 2019 3:29 pm

      Thanks for sharing this post,
      is very helpful article.

      Reply
    4. Suman Samanta on March 3, 2019 5:31 am

      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.

      Reply
      • Ben Dover on October 18, 2019 3:13 am

        Do you guys usually put fake comments on your own site in hopes people won’t figure it out?

        Reply
        • Rahul on October 18, 2019 4:16 am

          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.

          Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.