king.oslo Posted June 3, 2009 Share Posted June 3, 2009 Hello my friends, I am trying to make a Body Mass Index (BMI) calulator. It works so that users feed the calulator with their weight and height, and their BMI is calculated. Unfortunatly the way that people feed the calculator with their data seems to vary big time. For example, they may wright that their height is: 150cm 1.5meters ca 150cm 150 ..and so on. I need to find some functions or ways to get rid of letters, and convert the ones given in centimeters to meters. Which functions or methods can I use? Thanks Marius Link to comment https://forums.phpfreaks.com/topic/160808-extracting-and-converting-numbers-from-text-strings/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 3, 2009 Share Posted June 3, 2009 Don't give them a choice on how to enter the numbers. Use a drop-down select menu. Link to comment https://forums.phpfreaks.com/topic/160808-extracting-and-converting-numbers-from-text-strings/#findComment-848717 Share on other sites More sharing options...
ldougherty Posted June 3, 2009 Share Posted June 3, 2009 I'd personally have a text field for the number and a select box with cm, meter, etc then you use the information received from the form to do your conversions to whatever metric unit you want to use. Link to comment https://forums.phpfreaks.com/topic/160808-extracting-and-converting-numbers-from-text-strings/#findComment-848720 Share on other sites More sharing options...
king.oslo Posted June 3, 2009 Author Share Posted June 3, 2009 I thought about that, is there a way to do what I suggested, though? I thought it was more elegant Thanks, Marius Link to comment https://forums.phpfreaks.com/topic/160808-extracting-and-converting-numbers-from-text-strings/#findComment-848728 Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 preg_replace should work. Here's an example - preg_replace('#[^\d]#', '', $str); Link to comment https://forums.phpfreaks.com/topic/160808-extracting-and-converting-numbers-from-text-strings/#findComment-848743 Share on other sites More sharing options...
thebadbad Posted June 3, 2009 Share Posted June 3, 2009 But there's no foolproof solution, because you'd have to take every possible input into account. Here's my shot at a function: <?php function smart_convert_m($str) { preg_match('~^([\d,.]+)\s*([a-z.]+)$~iD', trim($str), $matches); $value = (float) str_replace(',', '.', $matches[1]); switch ($matches[2]) { case 'cm': case 'cm.': case 'centimetres': case 'centimeters': $m = $value / 100; break; case 'm': case 'm.': case 'metres': case 'meters': $m = $value; break; default: return false; } return $m; } //test with samples $samples = array('1,5 metres', '1.5meters', '1.5m', '150cm', '150centimetres', '150 centimeters'); foreach ($samples as $sample) { echo "$sample => " . smart_convert_m($sample) . '<br />'; } ?> Output: 1,5 metres => 1.5 1.5meters => 1.5 1.5m => 1.5 150cm => 1.5 150centimetres => 1.5 150 centimeters => 1.5 Edit: Added support for cm and m with a trailing dot. Link to comment https://forums.phpfreaks.com/topic/160808-extracting-and-converting-numbers-from-text-strings/#findComment-848755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.