Search code examples
phptrim

Remove first and last char from string


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?


Solution

  • 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.