phpexplodedelimited

Explode delimited string without creating empty elements


$string = "|1|2|3|4|";
$array = explode("|", $string, -1); 

foreach ($array as $part) {
    echo $part."-";
}

I use -1 in explode to skip the last "|" in string. But how do I do if I also want to skip the first "|"?


Solution

  • You can use trim to Strip | from the beginning and end of a string and then can use the explode.

    $string = "|1|2|3|4|";
    $array = explode("|", trim($string,'|'));