php - Best way to parse this string and create an array from it -
i have follow string:
{item1:test},{item2:hi},{another:please work}
what want turn array looks this:
[item1] => test [item2] => hi [another] => please work
here code using (which works):
$vf = '{item1:test},{item2:hi},{another:please work}'; $vf = ltrim($vf, '{'); $vf = rtrim($vf, '}'); $vf = explode('},{', $vf); foreach ($vf $vk => $vv) { $ve = explode(':', $vv); $vx[$ve[0]] = $ve[1]; }
my concern is; if value has colon in it? example, lets value item1
you:break
. colon going make me lose break
entirely. better way of coding in case value has colon in it?
why not set limit on explode function. this:
$ve = explode(':', $vv, 2);
this way string split @ first occurrence of colon.
Comments
Post a Comment