Skip to content

All topics  /  PHP

PHP String Remove Last Character Example

PHP ·

Hi Dev,

In this example, you will learn php string remove last character example. we will help you to give example of how to remove last character from string in php?. it's simple example of php remove last character from string. I explained simply about string remove last character php example.

There are four ways to remove last character from string in php. in this example, we will use to rtrim() function, substr_replace(), mb_substr(), substr() function remove last character from string.Let's see all examples with output.

Example 1:


index.php

<?php  	

   
   $str = "Hello World!";
   // Remove Last Character from String    
   $result = rtrim($str, "!");  
   echo $result;

   
?>

Output:

Hello World

Example 2:

index.php

<?php
   $str = "Nicesnippets.com?";

     
   // Remove Last Character from String
   $result = substr_replace($str ,"",-1);
   echo $result;
?>

Output:

Nicesnippets.com 

Example 3:

index.php

<?php    

   
   $str = "Mywebtuts.com>";
   // Remove Last Character from String 
   $result = mb_substr($str, 0, -1);
   echo $result;

   
?>

Output:

Mywebtuts.com 

Example 4:

index.php

<?php
   $str = "Hello Jaydeep?";
   // Remove Last Character from String 
   $result = substr($str, 0, -1); 

      
   echo  $result;
?>

Output:

Hello Jaydeep

I hope it could help you...