php replace string type parameter=value -


i have string comes db: $text=

parameter1=value1 parameter2=value2 otherparemeter=othervalue 

i need function replace parameter new value, if parameter not exist; must add string;

example: updatestring ($text,"parameter1","newvalue"):

result:

parameter1=newvalue parameter2=value2 otherparemeter=othervalue 

or: updatestring ($text,"myparameter","myvalue"):

result:

parameter1=value1 parameter2=value2 otherparemeter=othervalue myparameter=myvalue 

thanks !

given format of input parse_ini_string should work parse out data structure can altered , reformated.

maybe this:

$a =<<<eof parameter1=value1 parameter2=value2 otherparemeter=othervalue eof;  function replacevalue($text, $k, $v){     $dat = parse_ini_string($text);     $ret = "";     if(isset($dat[$k])){         $dat[$k] = $v;         foreach($dat $prop=>$val){             $ret.= $prop."=".$val."\n";         }         return $ret;     } } echo replacevalue($a, "parameter1", "hello world"); 

Comments