395

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
  • 510,633
  • 85
  • 743
  • 889
Dave
  • 8,879
  • 10
  • 33
  • 46

8 Answers8

796

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
  • 159,146
  • 25
  • 197
  • 199
89

umh.. like that?

$newstring = substr($dynamicstring, -7);
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
Vitaly Muminov
  • 1,914
  • 12
  • 11
25

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,255
  • 3
  • 27
  • 51
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') ;
alo Malbarez
  • 358
  • 2
  • 6
  • 16
MERT DOĞAN
  • 2,864
  • 26
  • 28
6

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

$new_string = substr( $dynamicstring, -min( strlen( $dynamicstring ), 7 ) );
alo Malbarez
  • 358
  • 2
  • 6
  • 16
mariovials
  • 782
  • 9
  • 12
6

for last 7 characters

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

$newstring : 5409els

for first 7 characters

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

$newstring : 2490slk

Grenoblois
  • 503
  • 1
  • 5
  • 19
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.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
-3

last 7 characters of a string:

$rest = substr( "abcdefghijklmnop", -7); // returns "jklmnop"
Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
keerthi
  • 5
  • 2