How to Get Last 2, 3, 4, 5, 7 Characters of a String in PHP?
Hello Dev,
Here, I will show you how to get last two characters of a string in php. We will use php get last 4 characters of string. you will learn php get last 3 characters of string. If you have a question about php get last 5 characters of string then I will give a simple example with a solution.
In this example, we will use substr() function to get last 2, 3, 4, 5 and 7 characters from string in php. so, let's see the following example code:
index.php
<?php
$string = "Welcome to ItSolutionstuff.com";
$last2Char = substr($string, -2);
var_dump($last2Char);
$last3Char = substr($string, -3);
var_dump($last3Char);
$last4Char = substr($string, -4);
var_dump($last4Char);
$last5Char = substr($string, -5);
var_dump($last5Char);
$last7Char = substr($string, -7);
var_dump($last7Char);
?>
Output:
string(2) "om"
string(3) "com"
string(4) ".com"
string(5) "f.com"
string(7) "uff.com"
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Upgrade PHP Version from 8.1 to 8.2 in Ubuntu?
- PHP Get First 2, 3, 4, 5, 10 Character from String Example
- How to Get First and Last Character from String in PHP?
- How to Get Last Character from String in PHP?
- How to Get First Character from String in PHP?
- How to Remove Last Character from String in PHP?
- How to Remove First Character from String in PHP?
- PHP Remove First and Last Character from String Example
- How to Upgrade PHP Version from 7.2 to 7.3 in Ubuntu?
- How to Partially Hide Email Address in PHP?
- How to Remove Multiple Keys from PHP Array?
- PHP MySQL Login with Google Account Example
- PHP Ajax Form Validation Example Tutorial
- How to Find Day Name from Specific Date in PHP?