php - get the number from a string then do the calculation -
i have string this
$coordinate = "coords='429, 457, 421, 460, 424, 464, 433, 465, 433, 460'";
i want multiply numbers 2, how can write simple php code calculation this?
this new $coordinate should be
coords="858, 914, 842, 920, 848, 928, 866, 930, 866, 920"
my orriginal string "alt='japan' shape='poly' coords='429, 457, 421, 460, 424, 464, 433, 465, 433, 460'";
something like:
$coords="429, 457, 421, 460, 424, 464, 433, 465, 433, 460"; $coords_arr = explode(",", $coords); array_walk($coords_arr, 'alter'); function alter(&$val) { $val *= 2; //multiply 2 } print_r($coords_arr);
updated code::
$coordinate = "coords='429, 457, 421, 460, 424, 464, 433, 465, 433, 460'"; $arr = explode("=", $coordinate); $data = trim($arr[1], "'"); //remove quotes start , end $coords=explode(",", $data); array_walk($coords, 'alter'); function alter(&$val) { $val = (int) $val * 2; } echo "<pre>"; print_r($coords);
Comments
Post a Comment