🌐
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 ...
🌐
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 use substr() to return part of a string. It lets you set where to start and how long to keep. ... This removes the last character (#) and returns David. It takes all characters from the start to the second-to-last one.
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
🌐
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 - Please read on. One way to strip the last character in a string is by using the PHP substr() function. The substr() function returns a portion of a string based on an offset and length (number of characters).
🌐
W3Docs
w3docs.com › php
How to Remove the Last Character from a String in PHP | W3Docs
To remove the last character, replace it with an empty string: ... <‌?php $string = "Hello World!"; echo "Given string: " . $string . "\n"; echo "Updated string: " . substr_replace($string, "", -1) .
🌐
Reactgo
reactgo.com › home › how to remove the last n characters of a string in php
How to remove the last n characters of a string in PHP | Reactgo
September 8, 2023 - To remove the last n characters of the string, we can use the built-in substr() function in PHP. The substr() function accepts three arguments, the first one is string, the second is start position, third is length.
🌐
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 ...
🌐
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; ?>
Find elsewhere
🌐
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 - Another effective way to remove the last character from a string in PHP is by using the substr() function. This function returns a part of a string based on specified start and length parameters.
🌐
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 &#8211; 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.
🌐
DEV Community
dev.to › patricia1988hernandez2 › remove-the-last-char-from-a-php-string-39eo
Remove the Last Char from a PHP String - DEV Community
April 17, 2025 - <?php $string = "My Office behind the Door.."; $replace = ""; $offset = -1; echo substr_replace( $string, $replace, $offset ); // The output: My Office behind the Door. ?> As you saw, the value was printed is the full sentence but deleted the last dot from the string.