PHP Empty Explode

Explode a string with no empty elements. Similar to C# Split String: StringSplitOptions.RemoveEmptyEntries.

function eexplode($separator, $string) {
    $array = explode($separator, $string);
    foreach ($array as $key => $val) {
        if (empty($val)) {
            unset($array[$key]);
        }
    }
    return $array;
}

NOTE: preg_split() using the PREG_SPLIT_NO_EMPTY flag is another option.

Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment