361

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
deceze
  • 491,798
  • 79
  • 706
  • 853
Dave
  • 8,315
  • 9
  • 32
  • 46

8 Answers8

745

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.

Community
  • 1
  • 1
Asaph
  • 154,284
  • 25
  • 189
  • 193
83

umh.. like that?

$newstring = substr($dynamicstring, -7);
Tomas Kubes
  • 21,982
  • 14
  • 105
  • 142
Vitaly Muminov
  • 1,844
  • 12
  • 11
22

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') ;
alo Malbarez
  • 354
  • 2
  • 6
  • 15
MERT DOĞAN
  • 2,470
  • 23
  • 25
20

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

Abdul Manan
  • 2,037
  • 3
  • 26
  • 51
5

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

$new_string = substr( $dynamicstring, -min( strlen( $dynamicstring ), 7 ) );
alo Malbarez
  • 354
  • 2
  • 6
  • 15
mariovials
  • 660
  • 10
  • 11
4

for last 7 characters

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

$newstring : 5409els

for first 7 characters

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

$newstring : 2490slk

Grenoblois
  • 368
  • 1
  • 3
  • 17
0

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.

ToolmakerSteve
  • 11,473
  • 10
  • 75
  • 168
-1

last 7 characters of a string:

$rest = substr( "abcdefghijklmnop", -7); // returns "jklmnop"
Rohit Suthar
  • 3,483
  • 1
  • 38
  • 47
keerthi
  • 9
  • 2