Remove first and last char from string

user248488 picture user248488 · Sep 11, 2010 · Viewed 46.5k times · Source

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?

Answer

NikiC picture NikiC · Sep 11, 2010

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.