• 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
  • VBA
  • 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 floor – Round Down and Find the Floor of a Number in php
  • 2.  php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function
  • 3.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 4.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 5.  php switch case – How to Use Switch…Case Statements in php
  • 6.  php acos – Find Arccosine and Inverse Cosine of Number
  • 7.  php array_pop() – Remove Last Element from Array
  • 8.  php asinh – Find Hyperbolic Arcsine of Number Using php asinh() Function
  • 9.  Replace Space with Dash in php String
  • 10.  php json_encode() Function – Get JSON Representation of Variables 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy