note 102673 added to function.explode

[email protected] Mon, 28 Feb 2011 04:03:43 -0800
Newsgroups php.notes
Message-ID <[email protected]>
An improvement to my previous post, this version will also trim whitespace from the front and end of the string, as well as allow you to specify a delimiter yourself.

<?php

/* Performs explode() on a string with the given delimiter and trims all whitespace for the elements */
function explode_trim($str, $delimiter = ',') {
    if ( is_string($delimiter) ) {
        $str = trim(preg_replace('|\\s*(?:' . preg_quote($delimiter) . ')\\s*|', $delimiter, $str));
        return explode($delimiter, $str);
    }
    return $str;
}

// Test
$str = '    mouse ,cat , dog  ,  human    ';
$array = explode_trim($str);
print_r($array);

?>

This will output:
(
    [0] => mouse
    [1] => cat
    [2] => dog
    [3] => human
)
----
Server IP: 69.147.83.197
Probable Submitter: 220.253.197.81
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit        -- https://master.php.net/note/edit/102673
Del: integrated  -- https://master.php.net/note/delete/102673/integrated
Del: useless     -- https://master.php.net/note/delete/102673/useless
Del: bad code    -- https://master.php.net/note/delete/102673/bad+code
Del: spam        -- https://master.php.net/note/delete/102673/spam
Del: non-english -- https://master.php.net/note/delete/102673/non-english
Del: in docs     -- https://master.php.net/note/delete/102673/in+docs
Del: other reasons-- https://master.php.net/note/delete/102673
Reject      -- https://master.php.net/note/reject/102673
Search      -- https://master.php.net/manage/user-notes.php