How Can I Remove the First or Last Character from a String in PHP?

Author — Nitish Kumar

Every now and then, we have to deal with strings which contain unwanted characters either at their beginning or the end. PHP provides a lot of methods which can be used to remove these characters. Each of them has its advantages and disadvantages. In this tutorial, we will discuss all these methods in detail so that you can choose the one that works best for your situation.

On This Page

Remove Last ‘n’ Characters from a String Using substr()

The PHP substr() function returns the part of string specified by the start and length parameters. If you want to remove characters from the end of string, you can set the value of start to 0 and the value of length to a negative number.

You can set the value of length to -1 in order to remove the last character of a string. Similarly, you can set the length to -2 in order to remove the last two characters from a string. To put it simply, set the value of length property to be negative of the number of characters that you want to remove from the end of the string.

In the following example, we have used the substr() function to remove different number of characters from the end of string.

PHP

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_last_one   = substr($sentence, 0, -1);
$removed_last_two   = substr($sentence, 0, -2);
$removed_last_three = substr($sentence, 0, -3);
$removed_last_four  = substr($sentence, 0, -4);
$removed_last_twenty  = substr($sentence, 0, -20);

// Output — An apple a day keeps anyone away if you throw it hard enough
echo $removed_last_one;

// Output — An apple a day keeps anyone away if you throw it hard enoug
echo $removed_last_two;

// Output — An apple a day keeps anyone away if you throw it hard enou
echo $removed_last_three;

// Output — An apple a day keeps anyone away if you throw it hard eno
echo $removed_last_four;

// Output — An apple a day keeps anyone away if you t
echo $removed_last_twenty;

Remove First ‘n’ Characters from a String Using substr()

You can also use the substr() function to remove characters from the beginning of a string by specifying a non-zero value for the length parameter. The length parameter is optional in this function. Omitting it will return the whole string starting from start index.

To remove the first character from the beginning of string, set the value of start to 1. To remove the first two characters from the beginning, set the value of start to 2. You can remove the first ‘n’ characters from a string by setting the value of start to ‘n’ in a similar manner.

PHP

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr($sentence, 1);
$removed_first_two   = substr($sentence, 2);
$removed_first_three = substr($sentence, 3);
$removed_first_four  = substr($sentence, 4);
$removed_first_twenty  = substr($sentence, 20);

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;

Remove Specific Characters from the End of String Using rtrim()

The rtrim() function in PHP strips away all characters from the end of given string which are specified in the character_mask. If you don’t set a value for character_mask, this function will remove NULL, tab, new line, vertical tab, carriage return and ordinary white space.

It is important to remember that rtrim() keeps removing characters from the end as long as they also exist in the character_mask. The following examples should make it clear:

PHP

$sentence = "How are you feeling Today????!!!,,,...   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim($sentence);

// Output — "How are you feeling Today????!!!,,,"
echo rtrim($sentence, ' .');

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim($sentence, ' ,');

// Output — "How are you feeling Today????!!!"
echo rtrim($sentence, ' .,');

// Output — "How are you feeling Today"
echo rtrim($sentence, '?!., ');

In the first case, we have not specified a value for the character_mask. Therefore, the rtrim() function only removes spaces from the end.

In the second case, the character_mask contains a space() and full stop (.). Therefore, the rtrim() function keeps removing the last characters as long as it is either a full stop or a space.

In the third case, the rtrim() function stops at the full stop because it is not included in the character mask.

In the last case, the rtrim() function keeps removing all the spaces, full stops, comma, exclamation and question marks until it reaches an alphabetic character.

Remove Specific Characters from the Beginning of String Using ltrim()

You can use the ltrim() function if you want to remove specific characters from the beginning of a string. This function works exactly like the rtrim() function but it remove characters present at the beginning.

PHP

$sentence = ",,,...!!!###???How are you feeling Today?";

// Output — "...!!!###???How are you feeling Today?"
echo ltrim($sentence, ',');

// Output — "!!!###???How are you feeling Today?"
echo ltrim($sentence, '.,');

// Output — "...!!!###???How are you feeling Today?"
echo ltrim($sentence, ',!');

// Output — "How are you feeling Today?"
echo ltrim($sentence, '?!.,#');

In the first case, the ltrim() function removes all the , that occur at the beginning of a the sentence or string. It is worth remembering that the function doesn’t stop after removing a single character. That is the reason it removed all the comma characters at the beginning and not just the first comma.

In the second case, the ltrim() function removed all the full stops and commas. It only stopped when it reached the exclamation marks.

The output in third and fourth case can be explained similarly.

Quick Summary

Let’s recap everything that we have covered in this tutorial.

  1. You can use the substr() function if you want to remove last ‘n’ characters from a string. All you have to do is provide a negative value for length.
  2. You can also remove the first ‘n’ characters from a string using the substr() function. This time, you will have to provide a value for the start parameter and leave length untouched.
  3. If you only want to remove some specific characters from the beginning of a string, you can use the ltrim() function. It will remove all occurrences of given characters irrespective of how many times they repeat.
  4. If you want to remove some specific characters from the end of a string, you can use the rtrim() function. It will also remove all occurrences of given characters irrespective of how many times they repeat.

Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know some other techniques to remove a given number of characters from the beginning or end of string in PHP.

Rate this post —

Very PoorPoorAverageGoodExcellent (No Ratings Yet)
Loading...
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kingwash
kingwash
4 years ago

Removing both first and last character

substr($str, 1, -1);

Example:

$str ="[some text here]"
substr($str, 1, -1);

output:

some text here
Last edited 4 years ago by Tutorialo Team
Tutorialo Team
Admin
Tutorialo Team
4 years ago
Reply to  kingwash

Thank you for your contribution @kingwash. I will add this technique to the main article crediting your comment. 🙂

0%