I have this:
$dataList = "*one*two*three*"; $list = explode("*", $dataList); echo"<pre>";print_r($list);echo"</pre>";
which outputs:
> Array ( > [0] => > [1] => one > [2] => two > [3] => three > [4] => )
How do I strip the fist and last * in the string before exploding?
The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With