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";
For simplicity, if you do not want send a message, try this
$new_string = substr( $dynamicstring, -min( strlen( $dynamicstring ), 7 ) );
for last 7 characters
$newstring = substr($dynamicstring, -7);
$newstring : 5409els
for first 7 characters
$newstring = substr($dynamicstring, 0, 7);
$newstring : 2490slk
umh.. like that?
$newstring = substr($dynamicstring, -7);
Safer results for working with multibyte character codes, allways use mb_substr instead substr. Example for utf-8:
$str = 'Ne zaman seni düsünsem';
echo substr( $str, -7 ) . ' <strong>is not equal to</strong> ' .
mb_substr( $str, -7, null, 'UTF-8') ;
last 7 characters of a string:
$rest = substr( "abcdefghijklmnop", -7); // returns "jklmnop"
Source: Stackoverflow.com