Jump to content

Strip string and add numbers


giraffemedia

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.