Params creator and reader


/ Published in: PHP
Save to your folder(s)

Simple function to create a params list type.

Example:
$array = array('first'=>'The first one','second'=>'The second one');

$string = parse_params($array);

//output first=the first one;second=the second one



$string = 'first=the first one;second=the second one';

$array = read_params($string);

//output array('first'=>'The first one','second'=>'The second one')


Copy this code and paste it in your HTML
  1. function read_params($params) {
  2. $params = explode(';',$params);
  3. foreach($params as $param) {
  4. $p = explode('=', $param);
  5. $pa[$p[0]] = $p[1];
  6. }
  7. $params = array_filter($pa);
  8. return $params;
  9. }
  10.  
  11. function parse_params($params,$gop=false) {
  12. if(is_array($params)){
  13. $d = array();
  14. foreach($params as $k=>$v){
  15. if(is_array($v)) $v = parse_params($v,true);
  16.  
  17. $d[] = ($gop ? '{'.$k.'='.$v.'}' : $k.'='.$v);
  18. }
  19. return implode( ($gop ? ',' : ';'),$d);
  20. }
  21. }

URL: www.censmedia.nl

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.