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 Quote Link to comment 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]); ?> Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted August 27, 2008 Share Posted August 27, 2008 /\d+(?:\.\d+)?/ Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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; ?> Quote Link to comment 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 Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.