I need to extract the number and the unit after the number from two different strings.. some strings have space between number and unit like this 150 g
and others don't 150g
$text = 'Rexona Ap Deo Aerosol 150ml Active CPD-05923';
$text = 'Cutex Nail Polish Remover Moisture 100ml ';
preg_match_all('!\d+!', $text, $matches);
if (sizeof($matches[0]) > 1) {
// how can I extract 'ml'
} else {
// how can I extract 150 ml ?
}
You can use:
preg_match_all('~\b(\d+(?:\.\d{1,2})?)\s*(ml|gm?|kg|cm)\b~i', $text, $matches);
And use matched groups #1 and #2.