CreateDynamicObject(2780, 33.62956, 2237.92529, 180.54372, 0.00000, 0.00000, 0.00000);
I want to know how can I parse the above line to get the values in variables as shown below:
$id = 2780;
$xPos = 33.62956;
$yPos = 2237.92529;
$zPos = 180.54372;
$rxpos= 0.0000;
$rypos = 0.00000;
$rzpos = 0.00000;
Since I would like to input a lot of lines like that and save it to an MySQL database.
There are a few ways to do this. One is just using basic string manipulation. If you remove the CreateDynamicObject(
and );
, then you're left with a comma-separated list. Use explode()
on it.
$str = 'CreateDynamicObject(2780, 33.62956, 2237.92529, 180.54372, 0.00000, 0.00000, 0.00000);';
$str = str_replace(array('CreateDynamicObject(', ');'), '', $str);
list($id, $xPos, $yPos, $zPos, $rxpos, $rypos, $rzpos) = explode(', ', $str);
If you have multiple, you can then just create an array to house all the values.
$strs = array(
'CreateDynamicObject(2780, 33.62956, 2237.92529, 180.54372, 0.00000, 0.00000, 0.00000);',
'CreateDynamicObject(2780, 33.62956, 2237.92529, 180.54372, 0.00000, 0.00000, 0.00000);'
);
$vals = array();
foreach($strs as $str){
$str = str_replace(array('CreateDynamicObject(', ');'), '', $str);
$row = array();
list(
$row['id'], $row['xPos'], $row['yPos'], $row['zPos'], $row['rxpos'], $row['rypos'], $row['rzpos']
) = explode(', ', $str);
$vals[] = $row;
}
var_dump($vals);