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
Mark Melgo
1,4871 gold badge11 silver badges29 bronze badges
answered Mar 28, 2019 at 6:08
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
NullPoiиteя
55.5k22 gold badges123 silver badges140 bronze badges
answered Feb 6, 2013 at 13:10
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. The PHP substr[] function returns the part of string specified by the You can set the value of In the following example, we have used the PHP You can also use the substr[] function to remove characters from the beginning of a string by specifying a non-zero value for the To remove the first character from the beginning of string, set the value of PHP The rtrim[] function in PHP strips away all characters from the end of given string which are specified in the It is
important to remember that PHP In the first case, we have not specified a value for the In the second case, the In the third case, the In the last case, the 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 PHP In the first case, the In the second case, the The output in third and fourth case can be explained similarly. Let’s
recap everything that we have covered in this tutorial. 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 Last ‘n’ Characters from a String Using substr[]
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.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.substr[]
function to remove different number of characters from the end of string.$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[]
length
parameter. The length
parameter is optional in this function. Omitting it will return
the whole string starting from start
index.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.$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[]
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.rtrim[]
keeps removing characters from the end as long as they also exist in the character_mask
. The following examples should make it clear:$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, '?!., '];
character_mask
. Therefore, the rtrim[]
function only removes spaces from the end.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.rtrim[]
function stops at the full stop because it is not included in the character mask.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[]
rtrim[]
function but it remove characters present at the beginning.$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, '?!.,#'];
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.ltrim[]
function removed all the full stops and commas. It only stopped when it reached the exclamation marks.Quick Summary
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
.substr[]
function. This time, you will have to provide a value for the start
parameter and leave length
untouched.ltrim[]
function. It will remove all occurrences of given characters irrespective of how many times they repeat.rtrim[]
function. It will also remove all occurrences of given characters irrespective of how many times they repeat.