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 - I’m going to show you about php 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.
IQCode
iqcode.com › code › php › string-remove-last-two-characters-php
string remove last two characters php Code Example
November 11, 2021 - //Remove the last character using substr $string = substr($string, 0, -1); ... $arrStr = 'Str1, Str2, str3, '; echo rtrim($arrStr, ", "); //Str1, Str2, str3 echo substr_replace($arrStr, "", -2); //Str1, Str2, str3 echo substr($arrStr, 0, -2); // Str1, Str2, str3 ...
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
Webstuffsolution
webstuffsolution.com › php-remove-last-character-from-string
PHP Remove last character from string (4 Methods)
September 19, 2024 - You can start at the 0′ the position and exclude the last character from string. Here’s how you can do it. substr(string $string, int $start, ?int $length= null): string ... ‘$length’ : Optional, If specified, this is the length of substring. If negative, it omits that characters from the end of the string · <?php $lst_string = "Welcome to WebStuffSolution!"; /* Remove Last Character in $lst_string */ $new_lst_string = substr($lst_string, 0, -1); echo $new_lst_string; ?>
Top answer 1 of 2
2
You can use substr function.
But if you will use preg_replace you can do this:
$val = preg_replace('/[\w\d]{2}$/', '', $val);
2 of 2
1
I'm pretty sure there are much easier ways to do this task, yet if we wish to use regular expressions, we would be starting with just a simple expression such as:
(.+)?(..)
if I understand the problem right, and our desired output is in this capturing group:
(.+)
Demo
$re = '/(.+)?(..)/m';
$str = '782A3
0012122
76542A
333333CD';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result;
RegEx Circuit
jex.im visualizes regular expressions:

Advice
AbraCadaver's advice in the comment is much better way:
substr('784XX', 0, -2);
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 - Does someone know any PHP function for that? ... You can use the function substr() provided by PHP for this. This function is giving you a sub string from an arbitrary string. As a first parameter, we are passing the input string, as a second parameter the starting position and the length is ...
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?
Decodingweb
decodingweb.dev › php-remove-last-character-from-string
How to remove the last character from a string in PHP
September 22, 2023 - One way to strip the last character in a string is by using the PHP substr() function. substr() function returns a portion of a string based on an offset and length (number of characters). <?php $var = 'abcdef'; $trimmed = substr($var, 0, 2); var_dump($trimmed); // output: string(2) "ab"
PHP
php.net › manual › en › function.rtrim.php
PHP: rtrim - Manual
This shows how rtrim works when using the optional charlist parameter: rtrim reads a character, one at a time, from the optional charlist parameter and compares it to the end of the str string. If the characters match, it trims it off and starts over again, looking at the "new" last character in the str string and compares it to the first character in the charlist again.
Hellonitish
hellonitish.com › blog › remove-first-or-last-character-from-a-string-in-php
Hello Nitish • How Can I Remove the First or Last Character from a String in PHP?
November 4, 2025 - If you want to remove characters from the end of string, you can set the value of start to 0 and the value of length to a negative number. You can set the value of length to -1 in order to remove the last character of a string. Similarly, you can set the length to -2 in order to remove the ...
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 .
Java2Blog
java2blog.com › home › php › remove last character from string in php
Remove Last Character from String in PHP [5 ways] - Java2Blog
December 12, 2022 - Here is an example PHP script that shows how to use the rtrim() function in such operation ... rtrim() function removes whitespace or specified characters from the end of a string, and takes two arguments ... To remove last character from string in PHP, we can make use of the mb_substr() function.