Remove first and last char from string

孤人 提交于 2019-12-08 14:32:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!