giraffemedia Posted August 27, 2008 Share Posted August 27, 2008 Hi I want to strip a string of all its letters and just leave the numbers. It contains a pair of comma separated values. Then I would like to multiply one number by the other. So the following... 16cm, 2 columns ...would be stripped to... 16,2 ...then multiply the remainder... 16 x 2 = 32 ...so that... $num = 32 How can I achieve this? I'm not sure if there is an expression that would do something like this. Regards James Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/ Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 No stripping is needed: <?php $str = '16cm, 2 columns'; preg_match_all('/\d+/', $str, $matches); echo $product = array_product($matches[0]); ?> Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626865 Share on other sites More sharing options...
giraffemedia Posted August 27, 2008 Author Share Posted August 27, 2008 Blimey, thanks effigy. How would I incorporate that into a while loop to perform the code on however many rows are returned then add the total? Something like... while ($row = mysql_fetch_array($classified_result, MYSQL_ASSOC)) { $maths = $row['bf_advert_size']; preg_match_all('/\d+/', $maths, $matches); echo $product = array_product($matches[0]) . '<br />'; } How can I add up each total returned? Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626871 Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 You mean a total of the totals? Create an array for the totals, push each one on, and use the same array_product approach array_sum. Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626878 Share on other sites More sharing options...
giraffemedia Posted August 27, 2008 Author Share Posted August 27, 2008 Never mind - i've just sorted it. I've used the following... while ($row = mysql_fetch_array($classified_result, MYSQL_ASSOC)) { $maths = $row['bf_advert_size']; preg_match_all('/\d+/', $maths, $matches); $product[] = array_product($matches[0]) . '<br />'; } echo array_sum($product); He he you just beat me to it effigy! Thanks for your help. James Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626879 Share on other sites More sharing options...
giraffemedia Posted August 27, 2008 Author Share Posted August 27, 2008 Just a quick question effigy, how can I change this to not strip out the decimal separator so I can use numbers such as 4.2 and 34.5? Regards James Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626927 Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 /\d+(?:\.\d+)?/ Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626933 Share on other sites More sharing options...
giraffemedia Posted August 27, 2008 Author Share Posted August 27, 2008 Brilliant!! ;D Thanks very much. Does anyone know anywhere that I can read more on pattern matching that is simple to understand for my brain!! James Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626942 Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 http://www.phpfreaks.com/forums/index.php/topic,127902.0.html Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626945 Share on other sites More sharing options...
giraffemedia Posted August 27, 2008 Author Share Posted August 27, 2008 Thanks effigy. I also found http://www.phpvideotutorials.com/regex/ which looks very good and more up my street than reading the php site. I find most of the php site information a bit above me and too complex at the moment but i'm sure i'll get there! Regards James Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626956 Share on other sites More sharing options...
giraffemedia Posted August 27, 2008 Author Share Posted August 27, 2008 Just a quick question effigy, how can I change this to not strip out the decimal separator so I can use numbers such as 4.2 and 34.5? Regards James He he - one more. What about if I was using fractions like 1/2, 2/3 etc. I've tried /\d+(?:\(/)\d+)?/ but it doesn't work. Anyone have an idea? James Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-626996 Share on other sites More sharing options...
discomatt Posted August 27, 2008 Share Posted August 27, 2008 You want to capture both fractions and decimals? '%\\d+(??:\\.\\d+)|(?:/\\d+))?%' And assuming you want to divide those numbers, you may want to do something like this <?php $str = '12 something 14 something else 12/3 pennies 4.6 foobars'; preg_match_all( '%\\d+(??:\\.\\d+)|(?:/\\d+))?%', $str, $matches ); eval( '$result = '.implode('*', $matches[0]).';' ); echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-627003 Share on other sites More sharing options...
giraffemedia Posted August 27, 2008 Author Share Posted August 27, 2008 No Matt, I would like to calculate just fractions but using the same principle. James Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-627005 Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 You must escape / because it is also serving as the pattern delimiter. <pre> <?php $str = '16cm, 2 columns 1/2'; preg_match_all('%\d+(?:[\./]\d+)?%', $str, $matches); foreach ($matches[0] as &$match) { if (strpos($match, '/') !== FALSE) { list($num, $den) = explode('/', $match); $match = eval("return $num / $den;"); } } echo $product = array_product($matches[0]); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-627007 Share on other sites More sharing options...
discomatt Posted August 27, 2008 Share Posted August 27, 2008 Character class... I'm an idiot. I knew the way i was doing it was wrong Link to comment https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/#findComment-627009 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.