How to Remove Last Character from String in PHP?

By Hardik Savani November 5, 2023 Category : PHP

Hello Artisan,

This example is focused on php remove last character from string. step by step explain how to remove last character from string in php. I would like to show you php remove last character from string example. This article will give you a simple example of remove last character from string php.

we will use substr() php function to remove last character from string. i will give you very simple examples to delete last character from string in php.

Example 1: PHP Remove Last Character from String

index.php

<?php

/* Declare string variable */

$myString = "Welcome to ItSolutionStuff.com!";

/* Remove Last Character of string */

$newString = substr($myStr, 0, -1);

/* Echo resulted string */

echo $newString;

?>

Output:

Welcome to ItSolutionStuff.com

Example 2: PHP Remove First Character from String

index.php

<?php

/* Declare string variable */

$myString = "Welcome to ItSolutionStuff.com!";

/* Remove First Character of string */

$newString = substr($myString, 1);

/* Echo resulted string */

echo $newString;

?>

Output:

elcome to ItSolutionStuff.com!

Example 3: PHP Remove First and Last Character from String

index.php

<?php

/* Declare string variable */

$myString = "Welcome to ItSolutionStuff.com!";

/* Remove First and Last Character of string */

$newString = substr($myStr, 1, -1);

/* Echo resulted string */

echo $newString;

?>

Output:

elcome to ItSolutionStuff.com

I hope it can help you...

Tags :
Shares