问题
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?
回答1:
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.
回答2:
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));
回答3:
trim($dataList, "*")
回答4:
$string = substr($dataList, 1, -1);
Remove the first and the last character of the string in php
回答5:
echo trim($dataList,"*");
hope this solve your problem
来源:https://stackoverflow.com/questions/3690470/remove-first-and-last-char-from-string