PHP Get Last 5 Characters from String Example

03-Apr-2023

.

Admin

PHP Get Last 5 Characters from String Example

Hi Dev,

In this tute, we will discuss php get last 5 Characters from string example. Here you will learn string to get last five Characters in php example. let’s discuss about get last five Characters from string in php example. We will look at example of how to use function to get last five Characters from string in php?. Follow bellow tutorial step of last five Characters get from string in php.

There are two example to get Last 5 Characters from string in PHP. in this example, we will use to substr() and mb_substr() function to get Last 5 Characters. so Let's both the following example with output.

Example 1: Get Last 5 Characters from String


index.php

<?php

$str = "Hello Developers";

//Get Last Five Characters from a string

$result = substr($str, strlen($str)-5);

echo $result;

?>

Output:

opers

Example 2: Get Last 5 Characters from String

index.php

<?php

$str = "Nicesnippets";

//Get Last Five Characters from a string

$result = mb_substr($str, -5);

echo $result;

?>

Output:

ppets

I hope it could help you...

#PHP