385

How would I go about grabbing the last 7 characters of the string below?

For example:

$dynamicstring = "2490slkj409slk5409els";
$newstring = some_function($dynamicstring);
echo "The new string is: " . $newstring;

Which would display:

The new string is: 5409els
0

8 Answers 8

782

Use substr() with a negative number for the 2nd argument.

$newstring = substr($dynamicstring, -7);

From the php docs:

string substr ( string $string , int $start [, int $length ] )

If start is negative, the returned string will start at the start'th character from the end of string.

3
  • 1
    But if $dynamicstring is less than 7 characters, $newstring will be empty. (at least on PHP 5.2.6)
    – Scott Chu
    Jan 29, 2016 at 8:07
  • 2
    You can always do a check with strlen first to see if the number of characters is greater than or equal to 7.
    – PBwebD
    Mar 29, 2016 at 13:32
  • 1
    Since PHP 8 you could use str_ends_with(string $haystack, string $needle): bool
    – Flo Bayer
    Sep 6, 2021 at 9:21
87

umh.. like that?

$newstring = substr($dynamicstring, -7);
0
24

It would be better to have a check before getting the string.

$newstring = substr($dynamicstring, -7);

if characters are greater then 7 return last 7 characters else return the provided string.

or do this if you need to return message or error if length is less then 7

$newstring = (strlen($dynamicstring)>=7)?substr($dynamicstring, -7):"message";

substr documentation

3
  • I think the else part is needed to give a message back. Something like invalid data entered or ..
    – Neri
    Nov 8, 2017 at 13:39
  • 2
    This is probably the best answer, only thing that might make it better is a reference to the php manual. Mar 9, 2018 at 19:32
  • 1
    In the second snippet, if strlen is exactly 7, it will return an error. FIX: Replace >7 with >=7. Jul 14, 2021 at 17:01
23

Safer results for working with multibyte character codes, allways use mb_substr instead substr. Example for utf-8:

$str = 'Ne zaman seni düşünsem';
echo substr( $str, -7 ) . ' <strong>is not equal to</strong> ' .
  mb_substr( $str, -7, null, 'UTF-8') ;
6

For simplicity, if you do not want send a message, try this

$new_string = substr( $dynamicstring, -min( strlen( $dynamicstring ), 7 ) );
6

for last 7 characters

$newstring = substr($dynamicstring, -7);

$newstring : 5409els

for first 7 characters

$newstring = substr($dynamicstring, 0, 7);

$newstring : 2490slk

1

There are multiple correct answers here. But it isn't obvious what is needed, if you want a "safe" version of substr,

Same as substr, when the string is "long enough", but if the string is too short, return the original string (instead of returning false).

/** Unlike substr, handles case where $string is too short.
 * @param $string
 * @param $nChars - negative to return at end of string.
 */
function safe_substr($string, $nChars) {
    if ($nChars == 0 || !isset($string))
        return "";

    if (strlen($string) <= abs($nChars))
        // $string is too short (or exactly the desired length). Return the string.
        return $string;

    return substr($string, $nChars);
}

NOTE: FOR UTF-8 chars, define safe_mb_substr, replacing substr above with mb_substr. And replace strlen with mb_strlen.

-3

last 7 characters of a string:

$rest = substr( "abcdefghijklmnop", -7); // returns "jklmnop"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.