Get Last Characters of a String

Posted by

Here, I will demonstrate how to retrieve the final two characters of a string using PHP. We will utilize PHP to get the string’s final four characters. Learn how to obtain the final three characters of a string in PHP. If you have a query regarding how to use PHP to retrieve the final five characters of a string, I will provide a brief example and a solution.

In this PHP example, we will utilize the substr() function to retrieve the string’s final 2, 3, 4, 5, and 7 characters. Consequently, let us examine the following example code:

<?php
  

	$string = "hii this is abhishek";
      
    $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) "ek"

string(3) "hek"

string(4) "shek"

string(5) "ishek"

string(7) "hishek"
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x