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
https://forums.phpfreaks.com/topic/121551-strip-string-and-add-numbers/
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?

 

 

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

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

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

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;

?>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.