Delete first 3 characters and last 3 characters from String PHP
Solution 1
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
Solution 2
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
Solution 3
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 **
Solution 4
substr($string, 3, strlen($string) - 6)
Solution 5
$myString='123456789';
$newString=substr($myString,3,-3);
Related videos on Youtube
Howdy_McGee
I'm a Web Developer who is constantly learning the trade. I've built and manage 100+ WordPress websites with the help of the talented Web Designers around me. I'm currently looking for ways I can help the WordPress community grow and maintain its awesomeness! ~ Alex
Updated on March 13, 2020Comments
-
Howdy_McGee over 4 years
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?
-
Howdy_McGee almost 13 yearsWell 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.
-
Shivang Gangadia almost 13 yearsit 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.
-
Howdy_McGee almost 13 yearsNo Sean Bright and others are right, when i originally commented to this the other answers were not there :S
-
Peter Meadley almost 7 yearsThis is a much more versatile answer than the accepted one, however accepted answer meets the required 3 character problem.