ItSolutionStuff.com

PHP Remove First and Last Character from String Example

By Hardik Savani • May 14, 2024
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: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check Query Execution Time in PHP?

Read Now →

How to Remove Special Characters from String in PHP?

Read Now →

How to Remove White Space from String in PHP?

Read Now →

PHP Check Word Exist in String or Not Example

Read Now →

How to Get a Current Page URL in PHP?

Read Now →

How to Increase Upload File Size Limit PHP in Ubuntu?

Read Now →

How to Check If String is URL or Not in PHP?

Read Now →

How to Convert Array Values to Lowercase in PHP?

Read Now →

How to Convert Object into Array in PHP?

Read Now →

How to Get Minimum Key Value of Array in PHP?

Read Now →

How to Count Number of Files in a Directory in PHP?

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →