How to get the last n characters of a PHP string? Last Updated : 31 Oct, 2018 Summarize Suggest changes Share 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 Advertise with us Next Article How to get the position of character in a string in PHP ? I imdhruvgupta Follow 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 How to get string between two characters in PHP ? A string is a sequence of characters stored in the incremental form in PHP. A set of characters, one or many can lie between any two chosen indexes of the string. The text between two characters in PHP can be extracted using the following two methods : Table of ContentUsing substr() Method: Using fo 4 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 How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_ 4 min read Article Tags : PHP Technical Scripter 2018 Like