regex - PHP: split string based on array -
below data i'm trying parse:
50‐59 1high300.00 avg300.00 90‐99 11high222.00 avg188.73 120‐1293high204.00 avg169.33
the first section weight range, next count, followed highprice, ending avgprice.
as example, need parse data above array like
[0]50-59 [1]1 [2]high300.00 [3]avg300.00 [0]90-99 [1]11 [2]high222.00 [3]avg188.73 [0]120‐129 [1]3 [2]high204.00 [3]avg169.33
i thought creating array of possible weight ranges can can't figure out how use values of array split string.
$arr = array("10-19","20-29","30-39","40-49","50-59","60-69","70-79","80-89","90-99","100-109","110-119","120-129","130-139","140-149","150-159","160-169","170-179","180-189","190-199","200-209","210-219","220-229","230-239","240-249","250-259","260-269","270-279","280-289","290-299","300-309");
any ideas appreciated.
hope work:
$string='50-59 1high300.00 avg300.00 90-99 11high222.00 avg188.73 120-129 3high204.00 avg169.33'; $requireddata=array(); $dataarray=explode("\n",$string); $counter=0; foreach($dataarray $data) { if(preg_match('#^([\d]+\-[\d]+) ([\d]+)([a-za-z]+[\d\.]+) ([a-za-z]+[\d\.]+)#', $data,$matches)) { $requireddata[$counter][]=$matches[1]; $requireddata[$counter][]=$matches[2]; $requireddata[$counter][]=$matches[3]; $requireddata[$counter][]=$matches[4]; $counter++; } } print_r($requireddata);
Comments
Post a Comment