I am importing a CSV file which contains measurements. The client writes these measurements in several formats...
eg:
569 - doesnt have unit, so need to append 'mm' by default. = 569mm
569" - has inches unit, so dont append 'mm'. = 569"
567+45 - has a double measurement, so need to recognise this as a number including the '+' and append 'mm'. = 567+45mm
567"+56" - double measurement with inches, dont append. = 567"+56"
567+56" - double measurement with inches, dont append (assume both are inches). = 567+56"
45Kg - already has unit, so dont append 'mm'. = 45Kg
Ive tried several ways: where csv[$i] is the current value... and assuming csv[$i] != '';
Code: Select all
if ($csv[$i] != '') {
//get the measurement (match numbers and + sign only)
if (preg_match('/\d+\+/', $csv[$i], $num_match)) {
$measurement = $num_match[0]
} else {
//client must have entered the unit without a meaurement
}
//get the unit (match any letter or special character, except plus sign)
if (preg_match('/\D+\W+[^\+]/', $csv[$i], $unit_match) ) {
$unit = $unit_match[0];
} else {
$unit = 'mm';
}
}
$csv[$i] = $measurement.$unit;