PHP Remove Last 2, 3, 4, 5, 10, etc Character From String Example

03-Apr-2023

.

Admin

PHP Remove Last 2, 3, 4, 5, 10, etc Character From String Example

Hi Dev,

This tutorial shows you how to remove last 2, 3, 4, 5, 10, etc character from string in php?. you can understand a concept of remove last 2, 3, etc character from string in php example. Here you will learn remove last character from string php.

There ara four ways to remove last 2, 3, 4, 5, etc Character From string in php. in this example, we will use to substr() function, mb_substr(), substr_replace(), and rtrim() function use this example.so Let's see the following example with output.

Example 1: PHP Remove Last 2 Character


index.php

<?php

$str = "Nicesnippets.com Hi";

// Remove Last 2,3,4, etc Character From string

$result = substr($str, 0, -2);

echo $result;

?>

Output:

Nicesnippets.com

Example 2: PHP Remove Last 3 Character

index.php

<?php

$str = "Hello mywebtuts.com";

// Remove Last 2,3,4, etc Character From string

$result = mb_substr($str, 0, -3);

echo $result;

?>

Output:

Hello mywebtuts.

Example 3: PHP Remove Last 4 Character

index.php

<?php

$str = "Hello Jaydeep";

// Remove Last 2,3,4, etc Character From string

$result = substr_replace($str,"",-4);

echo $result;

?>

Output:

Hello Jay

Example 4: PHP Remove Last 6 Character

index.php

<?php

$str = "Hello NicesnippetsAdmin!";

// Remove Last 2,3,4, etc Character From string

$result = rtrim($str, "Admin!");

echo $result;

?>

Output:

Hello Nicesnippets

I hope it could help you...

#PHP