How can I get the last 7 characters of a PHP string?

PhpString

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

umh.. like that?

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

Solution 3 - Php

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') ;

Solution 4 - Php

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

Solution 5 - Php

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

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

Solution 6 - Php

for last 7 characters

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

$newstring : 5409els

for first 7 characters

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

$newstring : 2490slk

Solution 7 - Php

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.

Solution 8 - Php

last 7 characters of a string:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - PhpAsaphView Answer on Stackoverflow
Solution 2 - PhpVitaly MuminovView Answer on Stackoverflow
Solution 3 - PhpMERT DOĞANView Answer on Stackoverflow
Solution 4 - PhpAbdul MananView Answer on Stackoverflow
Solution 5 - PhpmariovialsView Answer on Stackoverflow
Solution 6 - PhpGrenobloisView Answer on Stackoverflow
Solution 7 - PhpToolmakerSteveView Answer on Stackoverflow
Solution 8 - PhpkeerthiView Answer on Stackoverflow