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
- Iterate over characters of a string in Python
- Add index to characters and reverse the string
- How to remove all non-printable characters in a string in PHP?
- How to check a string begins with some given characters/pattern ?
- Convert List of Characters to String in Java
- Convert a String to a List of Characters in Java
- Python | Check if frequencies of all characters of a string are different
- Sum and Product of Prime Frequencies of Characters in a String
- Remove characters from string that appears strictly less than K times
- Lexicographically largest string formed from the characters in range L and R
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.