How can I remove three characters at the end of a string in PHP?
"abcabcabc" would become "abcabc"!
asked Feb 6, 2011 at 20:05
1
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
answered Feb 6, 2011 at 20:10
bensiubensiu
23.3k52 gold badges72 silver badges113 bronze badges
0
answered Feb 6, 2011 at 20:09
KomarSerjioKomarSerjio
2,7951 gold badge15 silver badges12 bronze badges
0
answered Feb 6, 2011 at 20:08
JanJan
2,2591 gold badge17 silver badges16 bronze badges
1
Question – How do I remove last character from a string in PHP script? In this tutorial you will learned multiple methods to remove last character from a string using php.
This tutorial describes 4 methods to remove last character from a string in PHP programming language. You can use any one of the following methods as per the requirements.
- Don’t Miss – Check If String Contains a Sub String in PHP
Method 1 – Using substr_replace function
You can use the PHP substr_replace[] function to remove the last character from a string in PHP.
Syntax:
substr_replace[$string,"",-1]; |
Example:
|
Output:
Original string: Hello TecAdmin! Updated string: Hello TecAdmin
Method 2 – substr function
Use the substr function to remove the last character from any string in PHP string
Syntax:
Example:
|
Output:
Original string: Hello TecAdmin! Updated string: Hello TecAdmin
Method 3 – mb_substr function
Use the mb_substr function to remove characters from the end of the string.
Syntax:
mb_substr[$string,0,-1]; |
Example:
|
Output:
Original string: Hello TecAdmin! Updated string: Hello TecAdmin
Method 4 – rtrim function
The rtrim function to used to remove specific characters from the end of the string.
Syntax:
Here “x” is the character to remove.
Example:
|
Output:
Original string: Hello TecAdmin! Updated string: Hello TecAdmin