How can i remove last 3 characters from a string in php?

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

How do I remove the last 3 characters from a string?

Use the String. slice[] method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice[0, -3]; . The slice method will return a new string that doesn't contain the last 3 characters of the original string.

How can I remove last 5 characters from a string in PHP?

How to Remove the Last Character from a String in PHP.
The First Option is Substr Function..
The Second Option is Substr_replace Function..
The Third Option is Rtrim[] Function..

How can I remove last character from a string in PHP?

You can use the PHP substr_replace[] function to remove the last character from a string in PHP. $string = "Hello TecAdmin!"; echo "Original string: " . $string .

How do you remove the last few characters of a string?

Using String. The easiest way is to use the built-in substring[] method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

Chủ Đề