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.

Answer from Asaph on Stack Overflow
🌐
W3Resource
w3resource.com › php-exercises › php-string-exercise-7.php
PHP String Exercise: Get the last three characters of a string - w3resource
March 31, 2026 - Uses the "substr()" function to extract the last 3 characters from the string stored in '$str1'. The "substr()" function takes two parameters: the string to extract from ($str1), and the starting position of the substring (-3, indicating the ...
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-get-last-2-characters-of-a-string-in-phpexample.html
How to Get Last 2, 3, 4, 5, 7 Characters of a String in PHP? - ItSolutionstuff.com
May 14, 2024 - Here, I will show you how to get last two characters of a string in php. We will use php get last 4 characters of string. you will learn php get last 3 characters of string.
🌐
Python Examples
pythonexamples.org › php › how-to-get-last-n-characters-in-string
How to get Last N Characters in String in PHP
<?php $str2 = "apple"; $last3Chars = substr($str2, -3); echo "Last 3 characters of the string are: $last3Chars"; ?>
🌐
PHP
php.net › manual › en › function.substr.php
PHP: substr - Manual
If you want to have a string BETWEEN two strings, just use this function: <?php function get_between($input, $start, $end) { $substr = substr($input, strlen($start)+strpos($input, $start), (strlen($input) - strpos($input, $end))*(-1)); return $substr; } //Example: $string = "123456789"; $a = "12"; $b = "9"; echo get_between($string, $a, $b); //Output: //345678 ?> ... This returns the portion of str specified by the start and length parameters.. It can performs multi-byte safe on number of characters. like mb_strcut() ... Note: 1.Use it like this bite_str(string str, int start, int length [,byte of on string]); 2.First character's position is 0. Second character position is 1, and so on... 3.$byte is one character length of your encoding, For example: utf-8 is "3", gb2312 and big5 is "2"...you can use the function strlen() get it...
🌐
Reactgo
reactgo.com › home › get the last n characters of a string in php
Get the last n characters of a string in PHP | Reactgo
September 1, 2023 - $string = "Google"; $lastthree = substr($string, -3); echo $lastthree; ... The substr() function accepts three arguments, the first one is string, the second is start position, third is length (which is optional).
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-get-the-last-character-of-a-string-in-php
How to get the last character of a string in PHP ? - GeeksforGeeks
July 23, 2025 - Output: From the end, "s" is at 1st position. We can display string from (-1) ... <?php $txt = "Akshit Loves GeeksForGeeks"; echo "Last character of String is: ".substr($txt, -1); ?>
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-last-n-characters-of-a-php-string
How to get the Last n Characters of a PHP String
July 31, 2023 - <?php $examples = [ "TutorialsPoint" ... 2 chars of 'PHP': HP Last 7 chars of 'WebDevelopment': opment · The substr() function with negative start position is the most efficient way to extract the last n characters from a PHP ...
🌐
Reactgo
reactgo.com › home › php: how to remove last 3 characters from a string
Php: How to remove last 3 characters from a string | Reactgo
March 5, 2023 - To remove the last three characters from a string, we can use the substr() function by passing the start and length as arguments.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-get-the-last-n-characters-of-a-php-string
How to get the last n characters of a PHP string? - GeeksforGeeks
October 31, 2018 - Method 1: Tn this method, traverse the last N characters of the string and keep appending them in a new string. Example: ... <?php $str = "GeeksforGeeks!"; $n = 6; // Starting index of the string // where the new string begins $start = strlen($str) - $n; // New string $str1 = ''; for ($x = $start; $x < strlen($str); $x++) { // Appending characters to the new string $str1 .= $str[$x]; } // Print new string echo $str1; ?>
🌐
Delft Stack
delftstack.com › home › howto › php › how to get the last char of a string in php
How to Get the Last Character of a String in PHP | Delft Stack
February 2, 2024 - We can get the last char of a string by considering it an array of chars. <?php $string = "This is a string"; $lengthOfString = strlen($string); $lastCharPosition = $lengthOfString-1; $lastChar = $string[$lastCharPosition]; echo "The last char of the string is $lastChar."; ...
🌐
W3Resource
w3resource.com › php-exercises › basic-algorithm › php-basic-algorithm-exercise-24.php
PHP Exercises: Convert the last 3 characters of a given string in upper case - w3resource
May 28, 2025 - <?php // Define a function that modifies a string based on its length function test($s) { // Check if the length of the string is less than 3 return strlen($s) < 3 ? strtoupper($s) : substr($s, 0, strlen($s) - 3) . strtoupper(substr($s, strlen($s) ...
🌐
Laracasts
laracasts.com › discuss › channels › laravel › how-to-get-last-characters-of-string
How to get last characters of string
substr($calendar->details, -100) $rest = substr("abcdef", -1); // returns "f" $rest = substr("abcdef", -2); // returns "ef" $rest = substr("abcdef", -3, 1); // returns "d"
🌐
sebhastian
sebhastian.com › php-get-last-character-of-string
How to get the last character of a string using PHP | sebhastian
November 23, 2022 - Characters in a string can be accessed using a zero-based index like an array: <?php $str = "It's a wonderful day"; // 👇 Get the last character of the string print $str[-1]; // Output: y
🌐
Designcise
designcise.com › web › tutorial › how-to-get-the-last-character-of-a-string-in-php
How to Get the Last Character of a String in PHP? - Designcise
June 4, 2020 - The reason behind this is because the regular substr() function is not multibyte-aware and would fail to detect the beginning or end of the multibyte character which would result in incorrect / corrupted result.
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-get-last-character-from-string-in-phpexample.html
How to Get Last Character from String in PHP? - ItSolutionstuff.com
May 14, 2024 - we will use substr() php function to get last character from string. i will give you very simple examples to get last character from string in php. ... I hope it can help you... ... I'm a full-stack developer, entrepreneur, and founder of ...
🌐
W3Schools
w3schools.com › php › › func_string_substr.asp
PHP substr() Function
The substr() function returns a part of a string. ... <?php echo substr("Hello world",10)."<br>"; echo substr("Hello world",1)."<br>"; echo substr("Hello world",3)."<br>"; echo substr("Hello world",7)."<br>"; echo substr("Hello world",-1)."<br>"; ...
🌐
CodeBasics
code-basics.com › programming › php course › extracting characters from a string
CodeBasics | Extracting characters from a string | PHP
That's why in programming it's customary to check the length of a string and access its characters only when it's safe. We'll get to that in future lessons. To get elements from the end, it's better to use negative indexes. In that case, counting starts from the end. <?php $firstName = 'Alexander'; print_r($firstName[-1]); // => r, the last character print_r($firstName[-2]); // => e, the second-to-last characterExpand code · String: 'H' 'e' 'x' 'l' 'e' 't' Index: 0 1 2 3 4 5 From end:-6 -5 -4 -3 -2 -1Expand code