I need to delete the first 3 letters of a string and the last 3 letters of a string. I know I can use substr() to start at a certain character but if I need to strip both first and last characters i'm not sure if I can actually use this. Any suggestions?
6 Answers
Pass a negative value as the length
argument (the 3rd argument) to substr()
, like:
$result = substr($string, 3, -3);
So this:
<?php
$string = "Sean Bright";
$string = substr($string, 3, -3);
echo $string;
?>
Outputs:
n Bri

- 118,630
- 17
- 138
- 146
Use
substr($var,1,-1)
this will always get first and last without having to use strlen.
Example:
<?php
$input = ",a,b,d,e,f,";
$output = substr($input, 1, -1);
echo $output;
?>
Output:
a,b,d,e,f

- 443
- 7
- 24

- 301
- 3
- 2
-
1This is a much more versatile answer than the accepted one, however accepted answer meets the required 3 character problem. – Peter Meadley Aug 19 '17 at 06:10
-
How is this answer ‘much more versatile’ than the accepted one? Please be specific. – Sean Bright Aug 10 '22 at 11:03
As stated in other answers you can use one of the following functions to reach your goal:
- substr($string, 3, -3) removes 3 chars from start and end
- trim($string, ",") removes all specific chars from start and end
- ltrim($string, ".") removes all specific chars from start
- rtrim($string, ";") removes all specific chars from end
It depends on the amount of chars you need to remove and if the removal needs to be specific. But finally substr()
answers your question perfectly.
Maybe someone thinks about removing the first/last char through string dereferencing. Forget that, it will not work as null
is a char as well:
<?php
$string = 'Stackoverflow';
var_dump($string);
$string[0] = null;
var_dump($string);
$string[0] = null;
var_dump($string);
echo ord($string[0]) . PHP_EOL;
$string[1] = '';
var_dump($string);
echo ord($string[1]) . PHP_EOL;
?>
returns:
string(13) "Stackoverflow"
string(13) "tackoverflow"
string(13) "tackoverflow"
0
string(13) "ackoverflow"
0
And it is not possible to use unset($string[0])
for strings:
Fatal error: Cannot unset string offsets in /usr/www/***.php on line **

- 5,867
- 2
- 50
- 77
I don't know php, but can't you take the length of the string, start as position 3 and take length-6 characters using substr?

- 21,690
- 12
- 62
- 94
-
Well if I do substr, it doesn't really take out the characters. If I do the substr($var, 3) it will start at the 3rd position. Then if I do the substr($var, -3) it will still have the first 3 characters in the string, and will end 3 characters early. I need it to remove characters entirely. – Howdy_McGee Aug 12 '11 at 19:58
-
it looks like Sean Bright has provided your answer. or did you mean to remove the characters from the original string? i know it's not technically the same thing, but you could replace your original variable with whatever substr returns. – Antony Scott Aug 12 '11 at 20:02
-
No Sean Bright and others are right, when i originally commented to this the other answers were not there :S – Howdy_McGee Aug 12 '11 at 20:04