I have the follow string:
{item1:test},{item2:hi},{another:please work}
What I want to do is turn it into an array that looks like this:
[item1] => test
[item2] => hi
[another] => please work
Here is the code I am currently using for that (which works):
$vf = '{item1:test},{item2:hi},{another:please work}';
$vf = ltrim($vf, '{');
$vf = rtrim($vf, '}');
$vf = explode('},{', $vf);
foreach ($vf as $vk => $vv)
{
$ve = explode(':', $vv);
$vx[$ve[0]] = $ve[1];
}
My concern is; what if the value has a colon in it? For example, lets say that the value for item1 is you:break. That colon is going to make me lose break entirely. What is a better way of coding this in case the value has a colon in it?
json_decode())$vf = '{item1:test},{item2:hi},{another:please work}'; $vf = str_replace(['{',':','}'], ['{"','":"','"}'], $vf); var_dump($vf); var_dump(json_decode('['.$vf.']', true));or$vf = '{item1:test},{item2:hi},{another:please work}'; $vf = str_replace(['},{','{',':','}'], ['","','{"','":"','"}'], $vf); var_dump($vf); var_dump(json_decode($vf, true));{colon:key:test}becomes{"colon":"key":"test"}?