Hi dev,
Today, i show you how to remove first and last character from string in php. This article will give you simple example of how to remove first and last character from string in php. you will how to remove first and last character from string in php. In this article, we will implement a how to remove first and last character from string in php.
we will use substr() php function to remove first and last character from string.
you can use this example with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version.
So, let’s follow few steps to create example of how to remove 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 codeplaners.com!"; /* Remove First and Last Character of string */ $newString = substr($myStr, 1, -1); /* Echo resulted string */ echo $newString; ?>
Output:
elcome to codeplaners.com
Example 2: PHP Remove First Character from String
index.php
<?php /* Declare string variable */ $myString = "Welcome to codeplaners.com!"; /* Remove First Character of string */ $newString = substr($myString, 1); /* Echo resulted string */ echo $newString; ?>
Output:
elcome to codeplaners.com!
Example 3: PHP Remove Last Character from String
index.php
<?php /* Declare string variable */ $myString = "Welcome to codeplaners.com!"; /* Remove Last Character of string */ $newString = substr($myStr, 0, -1); /* Echo resulted string */ echo $newString; ?>
Output:
Welcome to codeplaners.com
I hope it will assist you…