Use substr() with a negative number for the 2nd argument.
$newstring = substr($dynamicstring, -7);
From the php docs:
Answer from Asaph on Stack Overflow
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.
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 ...
Top answer 1 of 8
825
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.
2 of 8
92
umh.. like that?
$newstring = substr($dynamicstring, -7);
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...
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.
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; ?>
Iterrorsolution
iterrorsolution.com › post › get-last-2-3-4-5-characters-of-a-string-in-php-example.html
Get Last 2, 3, 4, 5 Characters of a String in PHP Example
We cannot provide a description for this page right now
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"
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