PHP Remove First and Last Character from String Example

By Hardik Savani November 5, 2023 Category : PHP

Hello Folks,

This tutorial will give you an example of php remove first and last character from string. We will look at an example of how to remove first and last character from string in php. Here you will learn php remove first and last character from string example. In this article, we will implement a remove first and last character from string php.

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

Example 1: 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

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 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

I hope it can help you...

Tags :
Shares