How to Get the Last Character of a String in PHP?

In PHP, there are multiple ways to retrieve the last character of a string. Whether you need to perform some specific operations on the last character or simply display it, PHP provides various functions and techniques to accomplish this task.

In this article, we'll explore different approaches to get the last character of a string in PHP.

1. Using the substr() Function

The substr() function in PHP is a versatile tool for working with substrings. To get the last character of a string, you can use substr() along with a negative offset.

Here's an example:

$inputString = "Hello, World!";
$lastCharacter = substr($inputString, -1);

echo $lastCharacter;

Output:

!

In this example, substr($inputString, -1) extracts the last character of the $inputString variable by using a negative offset of -1.

2. Using the mb_substr() Function for Multibyte Strings

If you're working with multibyte character encodings like UTF-8, it's important to use the mb_substr() function to ensure proper handling of multibyte characters.

Here's an example:

$inputString = "こんにちは、世界!";
$lastCharacter = mb_substr($inputString, -1);

echo $lastCharacter;

Output:

In this example, mb_substr($inputString, -1) extracts the last character of the multibyte string $inputString using a negative offset.

3. Using String Length and Indexing

You can also get the last character of a string by calculating its length and using array-like indexing.

Here's an example:

$inputString = "Hello, World!";
$stringLength = strlen($inputString);
$lastCharacter = $inputString[$stringLength - 1];

echo $lastCharacter;

Output:

!

In this example, strlen($inputString) calculates the length of the string, and then we access the last character using array indexing with $stringLength - 1.

4. Using mb_strlen() and Indexing for Multibyte Strings

For multibyte strings, you should use the mb_strlen() function to calculate the length of the string, followed by indexing.

Here's an example:

$inputString = "こんにちは、世界!";
$stringLength = mb_strlen($inputString);
$lastCharacter = $inputString[$stringLength - 1];

echo $lastCharacter;

Output:

In this example, we first calculate the length of the multibyte string using mb_strlen($inputString), and then we access the last character using array indexing with $stringLength - 1.

5. Using substr() for a Custom Number of Characters

If you need to get a specific number of characters from the end of a string, you can modify the substr() approach accordingly.

Here's an example that retrieves the last 3 characters:

$inputString = "Hello, World!";
$lastThreeCharacters = substr($inputString, -3);

echo $lastThreeCharacters;

Output:

d!!

In this example, substr($inputString, -3) extracts the last three characters of the string.

6. Using mb_substr() for a Custom Number of Characters in Multibyte Strings

For multibyte strings, use the mb_substr() function with a negative offset to extract a specific number of characters from the end.

Here's an example that retrieves the last 3 characters from a multibyte string:

$inputString = "こんにちは、世界!";
$lastThreeCharacters = mb_substr($inputString, -3);

echo $lastThreeCharacters;

Output:

世界!

In this example, mb_substr($inputString, -3) extracts the last three characters of the multibyte string.

Conclusion

Getting the last character or a specific number of characters from the end of a string in PHP can be achieved using various methods, depending on whether you're working with single-byte or multibyte character encodings. Understanding these techniques allows you to manipulate and work with the last characters of strings effectively in your PHP applications.