Just do:
echo substr($string, 0, -3);
You don't need to use a strlen call, since, as noted in the substr documentation:
Answer from bensiu on Stack OverflowIf length is given and is negative, then that many characters will be omitted from the end of string
Top answer 1 of 3
872
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
2 of 3
52
<?php echo substr("abcabcabc", 0, -3); ?>
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
Top answer 1 of 6
104
Pass a negative value as the length argument (the 3rd argument) to substr(), like:
$result = substr($string, 3, -3);
So this:
<?php
$string = "Sean Bright";
$string = substr($string, 3, -3);
echo $string;
?>
Outputs:
n Bri
2 of 6
29
Use
substr($var,1,-1)
this will always get first and last without having to use strlen.
Example:
<?php
$input = ",a,b,d,e,f,";
$output = substr($input, 1, -1);
echo $output;
?>
Output:
a,b,d,e,f
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
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.
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.
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