Write a PHP program to get last n characters of a given string.
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.
Method 2: Another way to do this use inbuilt library function substr with parameters as the name of the string.
Note: In above example, $start can also take -N to create a sub-string of last n characters
- Remove characters from a numeric string such that string becomes divisible by 8
- Check whether second string can be formed from characters of first string
- PHP | Different characters in the given string
- How to remove all non-printable characters in a string in PHP?
- Iterate over characters of a string in Python
- Count of 1-bit and 2-bit characters in the given binary string
- Add index to characters and reverse the string
- Convert a String to a List of Characters in Java
- Maximum repeated frequency of characters in a given string
- Convert List of Characters to String in Java
- How to check a string begins with some given characters/pattern ?
- Sum and Product of Prime Frequencies of Characters in a String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.