You are currently viewing 2 Easy Ways to Get Last Characters of String in PHP
  • Post category:PHP

2 Easy Ways to Get Last Characters of String in PHP

PHP Get Last Characters of String

The last characters of a string may need to be extracted to manipulate or analyze the data. So, in this article, we will see how to get the last characters of a string in PHP.

2 Ways to get the last characters of the string

We can get the last character using the substr() method and substr_replace() method.

1. Using substr() method

This function allows you to extract a substring from a given string. We should specify a negative start parameter indicating the number of characters from the end of the string to get the last characters.

Steps:

  1. Create the string in which you need to find the last character. Here we have named it ‘var1’
  2. Then you need to create another variable to store the last character of ‘var1’ string. We have named it the variable called ‘lastChar’.
  3. Assign ‘substr()’ function to ‘lastChar’ variable. Two values ​​should be passed in substr(), firstly the ‘variable name’ for which you want to find last character and secondly pass the negative number which how many last characters you want to get .
  4. Finally, print the variable ‘lastChar’ using echo. The variable ‘lastChar’ contains the last characters of ‘var1’.

Example 1:

$string = "WELCOME Home";
$lastChar = substr($string, -4);
echo $lastChar;

// Output: Home

Example 2:

$string = "WELCOME Home";
$lastChar = substr($string, -2);
echo $lastChar;

// Output: me

Example 3:

$string = "WELCOME Home";
$lastChar = substr($string, -1);
echo $lastChar;

// Output: e

2. using array_slice()

This method is little bit harder because in this method, we’re going to converts the string to an array using str_split() and then get last characters from string using arrray_slice().

READ ALSO  PHP Operator Precedence and Associativity

Steps:

  1. Create the string in which you need to find the last character. Here we have named it ‘var1’
  2. Then you need to create another variable to store the converted array. We have named it the variable called ‘Char’. Assign ‘Char’ to the str_split() method and pass the string/variable name.
  3. Then you need to create another variable to store the array containing the last characters. We have named it the variable called ‘lastChar’. Assign array_slice() method. Two values ​​should be passed in array_slice() , firstly the ‘converted array variable name’ and secondly pass negative number which is how many last characters you want to get.
  4. Finally, you need to create another variable to store the converted string with the last characters. We have named it the variable called ‘lastCharStr’. Assign implode() method, you need to pass two values one is an empty string (‘ ‘) and another one is a variable name that contains an array with the last characters.

Example 1:

$var1 = "Hello World";
$char = str_split($var1); // Converts the string to an array
$lastChar = array_slice($characters, -3); // Returns an array containing the last 3 characters: ["r", "l", "d"]
$lastCharStr = implode('', $lastCharacters); // Converts the array back to a string: "rld"

echo lastCharStr;
//Output : rld

Example 2:

$var1 = "Jackie chan";
$char = str_split($var1);
$lastChar = array_slice($characters, -4);
$lastCharaStr = implode('', $lastCharacters);
echo lastCharStr;

//Output :chan

Example 3:

$var1 = "Welcome Home";
$char = str_split($var1);
$lastChar = array_slice($characters, -4);
$lastCharaStr = implode('', $lastCharacters);
echo lastCharStr;

//Output : Home

Conclusion

You can use these methods to get last characters of string easily.

Leave a Reply