"PHP: Remove Last Character with substr() - Easy & Fast"

Sandra Jun 29, 2026

Mastering PHP Substr: Removing the Last Character

an image of two people in front of a computer screen with the text background removed
an image of two people in front of a computer screen with the text background removed

In the vast landscape of web development, PHP is a language that stands out for its simplicity and power. One of its many string manipulation functions is substr, which allows us to extract a substring from a given string. Today, we're going to explore how to use substr to remove the last character from a string in PHP.

an image of a cell phone screen with text on it and the words in different languages
an image of a cell phone screen with text on it and the words in different languages

Understanding substr() in PHP

Before we dive into removing the last character, let's first understand the basic syntax of the substr function. It takes three arguments:

Stop Resetting CSS the Wrong Way! | Utsav M.
Stop Resetting CSS the Wrong Way! | Utsav M.
  • $string: The original string from which we want to extract a substring.
  • $start: The position at which to start extracting the substring. This is an optional argument and defaults to 0 if not specified.
  • $length: The number of characters to extract. This is also an optional argument. If not specified, substr will extract the rest of the string from the starting position.

Removing the Last Character with substr()

How To : Remove passwords / editing restrictions on MS Word and Excel Files | Euclid Networks
How To : Remove passwords / editing restrictions on MS Word and Excel Files | Euclid Networks

To remove the last character from a string, we can use substr with a negative value for the second argument. This tells PHP to start from the end of the string and move backwards.

Here's how you can do it:

```php $string = "Hello, World!"; $string = substr($string, 0, -1); echo $string; // Outputs: Hello, World ```

Removing the Last Character with rtrim()

Anime Shadow Lord, Shadow Man Anime, Anime Character With Glowing Aura, Anime Shadow Power, Shadow Monarch Pfp, Moody Anime Guy, Anime Shadow Man, Dark Magician Profile Picture, Male Anime Villain Pfp
Anime Shadow Lord, Shadow Man Anime, Anime Character With Glowing Aura, Anime Shadow Power, Shadow Monarch Pfp, Moody Anime Guy, Anime Shadow Man, Dark Magician Profile Picture, Male Anime Villain Pfp

PHP provides another function called rtrim that can also be used to remove the last character from a string. This function removes trailing characters (optional parameter) from a string. If no optional parameter is provided, it removes whitespace characters.

Here's how you can use rtrim to remove the last character:

```php $string = "Hello, World!"; $string = rtrim($string, '!'); echo $string; // Outputs: Hello, World ```

Comparing substr() and rtrim()

the screenshoter is showing an image of a computer screen with text on it
the screenshoter is showing an image of a computer screen with text on it

Both substr and rtrim can be used to remove the last character from a string, but they have different use cases. substr is more versatile as it can extract any substring from a string, not just the last character. On the other hand, rtrim is specifically designed to remove trailing characters, making it more efficient for that particular task.

Practical Use Cases

Login With QR Code In PHP MYSQL
Login With QR Code In PHP MYSQL
🔤 Special Characters & Escape Characters
🔤 Special Characters & Escape Characters
the table has several different types of data in it, including numbers and symbols for each type
the table has several different types of data in it, including numbers and symbols for each type
PHP CRUD Tutorial with MySQL and Bootstrap 4 (Create, Read, Update and Delete)
PHP CRUD Tutorial with MySQL and Bootstrap 4 (Create, Read, Update and Delete)
Painbearer
Painbearer
5 Methods to Remove Read Only from Word Document
5 Methods to Remove Read Only from Word Document
a page with an image of a woman in armor
a page with an image of a woman in armor
Baldur's Gate Portraits, Pathfinder Character, The Evil Within, Creature Concept Art, Creature Concept, Dnd Characters, Character Portraits, Fantasy Character Design, Character Concept
Baldur's Gate Portraits, Pathfinder Character, The Evil Within, Creature Concept Art, Creature Concept, Dnd Characters, Character Portraits, Fantasy Character Design, Character Concept
Wyrin
Wyrin
three different types of characters standing next to each other
three different types of characters standing next to each other
absolver Dnd Assassin Vine, Druid Assassin, Masked Adventurer, Aragami 2 Wallpaper, Arcane Assassin, Aragami 2, Absolver Character Art, Wanderer Fantasy Art, Absolver City
absolver Dnd Assassin Vine, Druid Assassin, Masked Adventurer, Aragami 2 Wallpaper, Arcane Assassin, Aragami 2, Absolver Character Art, Wanderer Fantasy Art, Absolver City
a close up of a person with green eyes and black hair wearing a red dress
a close up of a person with green eyes and black hair wearing a red dress
mm
mm
DnD character
DnD character
an image of a man with blonde hair wearing a green vest and gold collared shirt
an image of a man with blonde hair wearing a green vest and gold collared shirt
User Registration With Email Verification In PHP MYSQL
User Registration With Email Verification In PHP MYSQL
Icewind Dale, Dragon's Dogma, Dishonored, Dnd Art, Fantasy Male, Rpg Games, Dnd Characters, Dark Fantasy, Character Art
Icewind Dale, Dragon's Dogma, Dishonored, Dnd Art, Fantasy Male, Rpg Games, Dnd Characters, Dark Fantasy, Character Art
the back side of an old computer screen with text on it and pictures of buildings
the back side of an old computer screen with text on it and pictures of buildings
an abstract painting of a man standing in front of a tree
an abstract painting of a man standing in front of a tree
Auto Resize Image To Fit In HTML CSS
Auto Resize Image To Fit In HTML CSS

Removing the last character from a string can be useful in various situations. Here are a few examples:

  • Removing a trailing comma or period from a string.
  • Removing the last character from a string to prevent it from causing issues in other operations, such as when using the string as a filename.
  • Removing the last character from a string to ensure it ends with a specific character, such as a space or a newline.

Best Practices

When using substr or rtrim to remove the last character, it's a good practice to check if the string is not empty before performing the operation. This can prevent potential errors or unexpected behavior. Here's an example:

```php $string = "Hello, World!"; if (!empty($string)) { $string = substr($string, 0, -1); } echo $string; // Outputs: Hello, World ```

This ensures that the operation is only performed if the string is not empty, preventing any potential issues that might arise from trying to manipulate an empty string.