Just do:

echo substr($string, 0, -3);

You don't need to use a strlen call, since, as noted in the substr documentation:

If length is given and is negative, then that many characters will be omitted from the end of string

Answer from bensiu on Stack Overflow
🌐
Reactgo
reactgo.com › home › php: how to remove last 3 characters from a string
Php: How to remove last 3 characters from a string | Reactgo
March 5, 2023 - php1min read · To remove the last three characters from a string, we can use the substr() function by passing the start and length as arguments. Here is an example, that removes the last three characters from a given string.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-program-to-remove-last-character-from-the-string
PHP Program to Remove Last Character from the String - GeeksforGeeks
July 23, 2025 - The substr() function in PHP can be used to return a part of a string. By specifying the start and length parameters, we can effectively remove the last character. The substr() function is used with the start parameter as 0 (beginning of the ...
People also ask

What if I want to remove the last character only when it matches a pattern?
Use preg_replace() with a regular expression like /character$/ to match it safely.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
Can I remove only slashes or commas from the end?
Yes. Use rtrim($string, "/,") to remove slashes or commas from the end only.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
How do I handle strings with emojis or accents?
Use mb_substr() and mb_strlen() to avoid breaking multibyte characters.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
🌐
FlatCoding
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
August 23, 2025 - You can remove the last character from a PHP string using four methods: substr_replace, substr, mb_substr, and rtrim. Here’s an example..
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-remove-last-2-3-4-5-7-characters-of-a-string-in-phpexample.html
How to Remove Last 2, 3, 4, 5, 7 Characters of a String in PHP? - ItSolutionstuff.com
May 14, 2024 - This article will give you a simple ... remove last 3 characters of string. you will learn php delete last 5 characters of string. In this example, we will use substr() function to remove last 2, 3, 4, 5 and 7 characters from string in php....
🌐
DEV Community
dev.to › lavary › how-to-remove-the-last-character-from-a-string-in-php-31ci
How to remove the last character from a string in PHP - DEV Community
February 6, 2023 - Another method to remove the last character from a string in PHP is by using the substr_replace() function.
🌐
PHP
php.net › manual › en › function.substr-replace.php
PHP: substr_replace - Manual
If you would like to remove characters from the start or end of a string, try the substr() function. For example, to remove the last three characters from a string: $string = "To be or not to be."; $string = substr ($string, 0, -3);
Find elsewhere
🌐
Decodingweb
decodingweb.dev › php-remove-last-character-from-string
How to remove the last character from a string in PHP
September 22, 2023 - PHP rtrim() accepts two arguments: the input string and characters you wish to be stripped from the end of the input string. For instance, to remove the last comma in a comma-separated list:
🌐
If Not True Then False
if-not-true-then-false.com › 2010 › php-remove-last-character-from-string
PHP: Remove Last Character from String – substr / substr_replace / rtrim :: If Not True Then False
October 3, 2010 - Method 1 – PHP: Remove Last Character from String using substr and mb_substr substr and mb_substr commands usage substr($string, 0, -1); mb_substr($string, 0, -1); substr and mb_substr example: $string = "This is test string.."; echo $string .
🌐
TecAdmin
tecadmin.net › remove-last-character-from-string-in-php
Remove Last Character from String in PHP (4 Methods) – TecAdmin
April 26, 2025 - Use PHP substr_replace() function to remove last charecter from string. How do I remove last character from a string in PHP script?
🌐
W3Schools
w3schools.com › php › func_string_rtrim.asp
PHP rtrim() Function
PHP Strings String Functions Modify Strings Concatenate Strings Slicing Strings Escape Characters PHP Numbers PHP Casting PHP Math PHP Constants PHP Magic Constants PHP Operators PHP If...Else...Elseif
🌐
sebhastian
sebhastian.com › php-remove-last-character-from-string
How to remove the last character from a PHP string | sebhastian
December 9, 2022 - Finally, another way to remove the last character from a string in PHP is to use the preg_replace() function.
🌐
W3docs
w3docs.com › home › code snippets › php › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP | W3docs
To remove the last character, specify a negative length: ... Note: For multibyte strings (e.g., UTF-8), use mb_substr($string, 0, -1, 'UTF-8') to avoid cutting characters in half.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-remove-the-last-character-from-a-string-in-php.php
How to Remove the Last Character from a String in PHP
PHP Array Functions PHP String Functions PHP File System Functions PHP Date/Time Functions PHP Calendar Functions PHP MySQLi Functions PHP Filters PHP Error Levels ... You can simply use the rtrim() function to remove the last character from ...
🌐
PrintMyFonts
askingbox.com › question › php-remove-last-x-characters-from-end-of-string
PHP: Remove last X Characters from End of String
April 19, 2015 - For example, I would like to shorten the string "abcdefg" by the last two characters, so that I will get "abcde" as a result. Does someone know any PHP function for that? ... You can use the function substr() provided by PHP for this.
🌐
Delft Stack
delftstack.com › home › howto › php › php remove last character from string
How to Remove the Last Character From a String in PHP | Delft Stack
March 13, 2025 - It includes the rtrim() function and the substr() function. Discover the best methods for string manipulation in PHP and enhance your programming skills with practical examples and clear explanations.
🌐
TheLinuxCode
thelinuxcode.com › home › php program to remove the last character from a string (practical, production-friendly guide)
PHP Program to Remove the Last Character from a String (Practical, Production-Friendly Guide) – TheLinuxCode
February 4, 2026 - If you accept real user input—emoji, accents, combining marks—then a byte-based approach can cut a character in half and produce invalid text.\n\nI’ll show you three core approaches you can run today (substr(), rtrim(), and substrreplace()), when I personally reach for each, and the pitfalls that bite teams in 2026: Unicode correctness, newline handling, and predictable behavior under tests and static analysis.\n\n## What “last character” really means in PHP\nPHP strings are byte sequences. Most of the common string functions (substr(), strlen(), substrreplace()) operate on bytes, no