To remove the first character from a string in php, the easiest way is with php substr() function.

$string = "This is a string variable";

$string_without_first_char = substr($string,1);

echo $string_without_first_char;

// Output:
his is a string variable

You can also use the php ltrim() function.

$string = "This is a string variable";

$string_without_first_char = ltrim($string,$string[0]);

echo $string_without_first_char;

// Output:
his is a string variable

When using string variables in php, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable.

For example, we can easily remove the first character from a string in php. To remove the first character from a string in php, we can use the substring function substr().

To remove the first character in a string using substr(), we know that the first character has index ‘0’. So, to get everything except the first character, we start our substring at the ‘1’ position.

Below is an example of how to get rid of the first character in a string using php.

$string = "This is a string variable";

$string_without_first_char = substr($string,1);

echo $string_without_first_char;

// Output:
his is a string variable

You can also use substr() to remove the last character from a string in php in a very similar way. To remove the last character in a string, pass ‘0’ as the start position and ‘-1’ as the end position.

$string = "This is a string variable";

$string_without_last_char = substr($string,0,-1);

echo $string_without_last_char;

// Output:
This is a string variabl

Using the php ltrim() Function to Remove Last Character from String

In php, the string method ltrim() removes the characters from the left side of the string that is given to it.

We can use the rtrim() method to remove the first character of a string by passing the first character of the string to the function.

Below is how you can use the ltrim() function in php to get rid of the first character in a string.

$string = "This is a string variable";

$string_without_first_char = ltrim($string,$string[0]);

echo $string_without_first_char;

// Output:
his is a string variable

How to Remove the First N Characters from String Using php

We can also use the substr() function to get rid of the first n characters of a string easily in php.

To remove the first n characters from a string using php, you just need to change the input to the end position argument of the slice you want.

For example, to remove the first 5 characters from a string, you pass ‘5’ as shown in the following php code.

$string = "This is a string variable";

$string_without_first_5_char = substr($string,5);

echo string_without_first_5_char;

// Output:
is a string variable

How to Remove the Last and Last Character from a String Using php

Another example of how we can use substr() to remove characters from a string in php is the removal of the first and first character from a string.

With substr()again, we can easily get rid of the first and last character from a string.

To do so, we need to select all of the elements between the first and last character.

Below is how we can remove the first and last character from a string in php.

$string = "This is a string variable";

$string_without_first_or_first_char = substr($string,1,-1);

echo $string_without_first_or_first_char;

// Output:
his is a string variabl

How to Remove a Character at a Specific Position in a String Using php

The first example I want to share with you in this article is how to remove a character from a string at a specific position.

Let’s say we want to remove the sixth character from a string.

To do so, we can create two substrings from our original string and then concatenate them. We first create a string from the start of our string to the desired position, and then create another string from the desired position + 1 to the end.

Below is an example of how to use substr() in php to remove a character at a specific position.

$string = "This is a string variable";

$string_without_sixth_char = substr($string,0,6) . substr($string,6+1);

echo $string_without_sixth_char;

// Output:
This i a string variable

Hopefully this article has been beneficial for you to learn how to remove the first character from a string in your php code.

Categorized in:

PHP,

Last Update: March 13, 2024