从string中删除第一个和最后一个char

时间:2022-08-28 20:15:42

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?

如何在爆炸前剥去弦中的第一个和最后一个*?

4 个解决方案

#1


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.

这将从字符串的结尾和开头删除所有*字符(即使有多个!)。

#2


17  

Some other possibilities:

其他一些可能性:

Using substr:

使用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():

您也可以选择不从字符串中删除*,而是删除始终是第一个和最后一个元素的空数组值。使用数组函数array_pop()和array_shift():

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

#3


6  

trim($dataList, "*")

#4


1  

echo trim($dataList,"*");

hope this solve your problem

希望这能解决你的问题

#1


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.

这将从字符串的结尾和开头删除所有*字符(即使有多个!)。

#2


17  

Some other possibilities:

其他一些可能性:

Using substr:

使用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():

您也可以选择不从字符串中删除*,而是删除始终是第一个和最后一个元素的空数组值。使用数组函数array_pop()和array_shift():

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

#3


6  

trim($dataList, "*")

#4


1  

echo trim($dataList,"*");

hope this solve your problem

希望这能解决你的问题