Delete first 3 characters and last 3 characters from String PHP

Howdy_McGee picture Howdy_McGee · Aug 12, 2011 · Viewed 82.3k times · Source

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?

Answer

Sean Bright picture Sean Bright · Aug 12, 2011

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