PHP and javascript snippets you can copy and paste.

Wednesday, March 28, 2007

str_explode_strAssoc($separator,$assignSeparator,$str)

/**
* Works like 'expode()' but parses the string into an associative array where the keys are the
* values on the left of $assignSeparator and the values are the values on the right of $assignSeparator.
* eg if given explodeAssoc(',','=','Content-Type=text/plain,...')
* then this method would return:
* array('Content-Type'=>'text/plain', ...)
* @param string $separator
* @param string $asssignSeparator
* @param string $str
*/

function str_explode_strAssoc($separator,$assignSeparator,$str){

$assocArr = array();

$arr = explode($separator,$str); //eg array('Content-Type=text/plain',...)
while(list($key,$val)=each($arr)){
$temp = explode($assignSeparator,$val);
$assocArr[$temp[0]] = isset($temp[1])?$temp[1]:'';
}
return $assocArr;

}

No comments: