ItSolutionStuff.com

How to Get Last 2, 3, 4, 5, 7 Characters of a String in PHP?

By Hardik Savani • May 14, 2024
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...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Upgrade PHP Version from 8.1 to 8.2 in Ubuntu?

Read Now →

PHP Get First 2, 3, 4, 5, 10 Character from String Example

Read Now →

How to Get First and Last Character from String in PHP?

Read Now →

How to Get Last Character from String in PHP?

Read Now →

How to Get First Character from String in PHP?

Read Now →

How to Remove Last Character from String in PHP?

Read Now →

How to Remove First Character from String in PHP?

Read Now →

PHP Remove First and Last Character from String Example

Read Now →

How to Upgrade PHP Version from 7.2 to 7.3 in Ubuntu?

Read Now →

How to Partially Hide Email Address in PHP?

Read Now →

How to Remove Multiple Keys from PHP Array?

Read Now →

PHP MySQL Login with Google Account Example

Read Now →

PHP Ajax Form Validation Example Tutorial

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →