Remove last 4 characters from string 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

To remove the last three characters from a string, we can use the substr[] function by passing the start and length as arguments.

Here is an example, that removes the last three characters from a given string.

$name="John res";
//if length is negative it starts removing from the end
$result = substr[$name,0,-3];
echo $result;

Output:

John

The substr[] function doesn’t mutate the original string, instead of it creates a new string the modified data.

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 can I remove last 4 characters from a string in PHP?

You can use the PHP rtrim[] function to remove the last character from the given string in PHP.

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

To remove the last three characters from a string, we can use the substr[] function by passing the start and length as arguments.

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 I remove the first 4 characters of a string?

There are three ways in JavaScript to remove the first character from a string:.
Using substring[] method. The substring[] method returns the part of the string between the specified indexes or to the end of the string. ... .
Using slice[] method. ... .
Using substr[] method..

Chủ Đề