Search code examples
phpstringcharacterstripsubstr

Delete first 3 characters and last 3 characters from String PHP


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?


Solution

  • 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