Remove first and last character from string php

I have this:

$dataList = "*one*two*three*";
$list = explode("*", $dataList);
echo"
";print_r($list);echo"
";

which outputs:

> Array (
>     [0] => 
>     [1] => one
>     [2] => two
>     [3] => three
>     [4] =>  )

How do I strip the fist and last * in the string before exploding?

NikiC

99.5k36 gold badges188 silver badges224 bronze badges

asked Sep 11, 2010 at 9:35

Using trim:

trim($dataList, '*');

This will remove all * characters (even if there are more than one!) from the end and the beginning of the string.

answered Sep 11, 2010 at 9:37

NikiCNikiC

99.5k36 gold badges188 silver badges224 bronze badges

1

Some other possibilities:

Using substr:

$dataList = substr($dataList, 1, -1);

You can also choose to not remove the * from the string but rather remove the empty array values which will always be the first and last element. Using array functions array_pop() and array_shift():

$arrData = array_pop(array_shift($arrData));

answered Oct 23, 2012 at 12:17

BojoerBojoer

4814 silver badges4 bronze badges

1

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

Remove the first and the last character of the string in php

Remove first and last character from string php

Mark Melgo

1,4871 gold badge11 silver badges29 bronze badges

answered Mar 28, 2019 at 6:08

Remove first and last character from string php

Syed SxxisSyed Sxxis

1711 silver badge2 bronze badges

answered Sep 11, 2010 at 9:37

reko_treko_t

54.2k10 gold badges85 silver badges77 bronze badges

echo trim($dataList,"*");

hope this solve your problem

Remove first and last character from string php

NullPoiиteя

55.5k22 gold badges123 silver badges140 bronze badges

answered Feb 6, 2013 at 13:10

Last Updated — April 1, 2022

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.

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 —

Remove first and last character from string php
Loading...

How do I remove the first and last character of a string in PHP?

Using trim : trim($dataList, '*'); This will remove all * characters (even if there are more than one!) from the end and the beginning of the string.

How can I remove last 4 characters from a string in PHP?

You can use the PHP rtrim() function to remove the last character from the given string in PHP.

What is PHP Ltrim?

The ltrim() function removes whitespace or other predefined characters from the left side of a string. Related functions: rtrim() - Removes whitespace or other predefined characters from the right side of a string. trim() - Removes whitespace or other predefined characters from both sides of a string.

How do I remove the last character of a string?

How to Remove Last Character from String in Java.
Using StringBuffer.deleteCahrAt() Class..
Using String.substring() Method..
Using StringUtils.chop() Method..
Using Regular Expression..