Convert a name=value pairs string into array





11
Date Submitted Wed. Jul. 5th, 2006 8:15 AM
Revision 2 of 2
Helper poncho
Tags Array | Name | PHP | String | Value
Comments 3 comments
Convert an irregular name=value pair string into a formatted array.
function pairstr2Arr ($str, $separator='=', $delim=',') {
    $elems = explode($delim, $str);
    foreach( $elems as $elem => $val ) {
        $val = trim($val);
        $nameVal[] = explode($separator, $val);
        $arr[trim(strtolower($nameVal[$elem][0]))] = trim($nameVal[$elem][1]);
    }
        return $arr;
}

//  Example usage:
$string = '1=one, 2=two , 3=three , Site=apple , name= Joe Bloggs';
$array = pairstr2Arr($string);

echo '<pre>';
print_r($array);
echo '</pre>';>

//  Returns:
Array
(
    [1] => one
    [2] => two
    [3] => three
    [site] => apple
    [name] => Joe Bloggs
)

Mark Thompson

www.defaultstate.com/
Perfecting the art of breaking stuff!

Comments

Comments ...
Fri. Jul. 21st, 2006 12:48 AM    Beginner hello2usir
  Comments Beat me
Tue. Aug. 8th, 2006 8:29 PM    Beginner schmalls
Comments Example of input and output
Thu. Feb. 16th, 2006 7:00 AM    Beginner nev3rm0re

Voting