• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Remove Last Character from String Using php

Remove Last Character from String Using php

March 13, 2022 Leave a Comment

To remove the last character from a string in php, the easiest way is with php substr() function.

$string = "This is a string variable";

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

echo $string_without_last_char;

// Output:
This is a string variabl

You can also use the php rtrim() function.

$string = "This is a string variable";

$string_without_last_char = rtrim($string,$string[-1]);

echo $string_without_last_char;

// Output:
This is a string variabl

When using string variables in php, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable.

For example, we can easily remove the last character from a string in php. To remove the last character from a string in php, we can use the substring function substr().

To remove the last character in a string using substr(), we know that the last character has index ‘-1’. So, to get everything except the last character, we want to get everything up until the last position, or the ‘-1’ position.

Below is an example of how to get rid of the last character in a string using php.

$string = "This is a string variable";

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

echo $string_without_last_char;

// Output:
This is a string variabl

You can also use substr() to remove the first character from a string in php in a very similar way. To remove the first character in a string, pass ‘1’ as the start position.

$string = "This is a string variable";

$string_without_first_char = substr($string, 1);

echo $string_without_first_char;

// Output:
his is a string variable

Using the php rtrim() Function to Remove Last Character from String

In php, the string method rtrim() removes the characters from the right side of the string that is given to it.

We can use the rtrim() method to remove the last character of a string by passing the last character of the string to the function.

Below is how you can use the rtrim() function in php to get rid of the last character in a string.

$string = "This is a string variable";

$string_without_last_char = rtrim($string,$string[-1]);

echo $string_without_last_char;

// Output:
This is a string variabl

How to Remove the Last N Characters from String Using php

We can also use the substr() function to get rid of the last n characters of a string easily in php.

To remove the last n characters from a string using php, you just need to change the input to the end position argument of the slice you want.

For example, to remove the last 5 characters from a string, you pass ‘-5’ as shown in the following php code.

$string = "This is a string variable";

$string_without_last_5_char = substr($string,0,-5);

echo string_without_last_5_char;

// Output:
This is a string var

How to Remove the Last and Last Character from a String Using php

Another example of how we can use substr() to remove characters from a string in php is the removal of the first and last character from a string.

With substr()again, we can easily get rid of the first and last character from a string.

To do so, we need to select all of the elements between the first and last character.

Below is how we can remove the first and last character from a string in php.

$string = "This is a string variable";

$string_without_first_or_last_char = substr($string,1,-1);

echo $string_without_first_or_last_char;

// Output:
his is a string variabl

How to Remove a Character at a Specific Position in a String Using php

The last example I want to share with you in this article is how to remove a character from a string at a specific position.

Let’s say we want to remove the sixth character from a string.

To do so, we can create two substrings from our original string and then concatenate them. We first create a string from the start of our string to the desired position, and then create another string from the desired position + 1 to the end.

Below is an example of how to use substr() in php to remove a character at a specific position.

$string = "This is a string variable";

$string_without_sixth_char = substr($string,0,6) . substr($string,6+1);

echo $string_without_sixth_char;

// Output:
This i a string variable

Hopefully this article has been beneficial for you to learn how to remove the last character from a string in your php code.

Other Articles You'll Also Like:

  • 1.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 2.  Add Months to Date in php
  • 3.  php range() – Create Array of Elements Within Given Range
  • 4.  php str_replace – Replace String in php
  • 5.  php print_r() Function- Get Information about Variables in php
  • 6.  Using strtoupper() in php to Convert String to Uppercase
  • 7.  php var_dump() Function – Print Information about Variable Type and Value
  • 8.  php array_diff() – Find Elements of Array Not Present in Other Arrays
  • 9.  How to Parse URLs with php parse_url() Function
  • 10.  How to Check If String Contains Substring in PHP

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy