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
- How to remove all non-printable characters in a string in PHP?
- Add index to characters and reverse the string
- Count of 1-bit and 2-bit characters in the given binary string
- How to check a string begins with some given characters/pattern ?
- Python | Check if frequencies of all characters of a string are different
- Convert a String to a List of Characters in Java
- Sum and Product of Prime Frequencies of Characters in a String
- Convert List of Characters to String in Java
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.