mme Posted October 3, 2010 Share Posted October 3, 2010 Hi, I currently have a string with some values in it: $a_random_string="some random text, goes here - $123<br>some more random text goes here - $53 (text 123 )<br>ANother line of text - $126"; Now I want to add all the values that have a '$' before the value eg; $123. So in this case I would end up with: 302 How would I do this? Thanks, mme Quote Link to comment https://forums.phpfreaks.com/topic/215022-adding-values-in-a-string/ Share on other sites More sharing options...
Lamez Posted October 3, 2010 Share Posted October 3, 2010 Can you elaborate a little more? Quote Link to comment https://forums.phpfreaks.com/topic/215022-adding-values-in-a-string/#findComment-1118478 Share on other sites More sharing options...
mme Posted October 3, 2010 Author Share Posted October 3, 2010 Thank you for your reply, Basically I have a string of variable data in it and I want to add up all the numeric values in the string that have the $ character in front of the number. However I do not want the numbers with the $ character before the number to be added. eg; blabla 621. - Dont include this more text $63. with some more text 12. - Include the number 63 but not the number 12 Hope that helps. Thanks, mme Quote Link to comment https://forums.phpfreaks.com/topic/215022-adding-values-in-a-string/#findComment-1118479 Share on other sites More sharing options...
Lamez Posted October 3, 2010 Share Posted October 3, 2010 I see, your algorithm should go as follows: set empty total variable parse string, when finding a 'word' with a $ add the number to the total return total. Quote Link to comment https://forums.phpfreaks.com/topic/215022-adding-values-in-a-string/#findComment-1118481 Share on other sites More sharing options...
mme Posted October 3, 2010 Author Share Posted October 3, 2010 Thanks for the reply, What function could I use to get the 'word' out of the string? Thanks, mme Quote Link to comment https://forums.phpfreaks.com/topic/215022-adding-values-in-a-string/#findComment-1118485 Share on other sites More sharing options...
PaulRyan Posted October 3, 2010 Share Posted October 3, 2010 Hey mme, have a look at the following... <?php // String to get values from $startString = 'some random text, goes here - $123<br>some more random text goes here - $53 (text 123 )<br>ANother line of text - $126'; // Add | to all values that begin with $ and contain numbers $string = preg_replace('/(\$[0-9]{1,})/','|$1|',$startString); // Explode | $string = explode('|',$string); // Count Length of exploded string $count = count($string); // Preg match for any values not contain a space // If no space is found, it must be a number beginning with $ // Replace $ and then add the values up for($i=0; $i<$count; $i++) { if(!preg_match("/ /i", $string[$i])) { $result += str_replace('$','',$string[$i]); } } // Echo resulting value echo $result; ?> Tell me if it works out for you bud Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/215022-adding-values-in-a-string/#findComment-1118489 Share on other sites More sharing options...
mme Posted October 3, 2010 Author Share Posted October 3, 2010 Thank you so much. I have also changed it a little to include decimals. But everything is working now Thanks so Much -mme Quote Link to comment https://forums.phpfreaks.com/topic/215022-adding-values-in-a-string/#findComment-1118496 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.