How to get the last n characters of a PHP string? Last Updated : 31 Oct, 2018 Comments Improve Suggest changes Like Article Like Report Write a PHP program to get last n characters of a given string. Examples: Input : $str = "GeeksforGeeks!" $n = 6 Output : Geeks! Input : $str = "GeeksforGeeks!" $n = 9 Output : forGeeks! Method 1: Tn this method, traverse the last N characters of the string and keep appending them in a new string. Example: php <?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; ?> Output: Geeks! Method 2: Another way to do this use inbuilt library function substr with parameters as the name of the string. Example: php <?php $str = "GeeksforGeeks!"; $n = 6; $start = strlen($str) - $n; // substr returns the new string. $str1 = substr($str, $start); echo $str1; ?> Output: Geeks! Note: In above example, $start can also take -N to create a sub-string of last n characters Comment More infoAdvertise with us Next Article How to get the position of character in a string in PHP ? I imdhruvgupta Follow Improve Article Tags : PHP Technical Scripter 2018 Similar Reads How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in 2 min read How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t 2 min read JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re 3 min read Like