finding numbers and adding them

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

finding numbers and adding them

Post by take2hikes »

Alright.. I am reading the posts in this forum regarding reg expressions. I'm not getting anything to really work on my server. I'm a newb to PHP and I'm trying to do the following:

Search a 'blob' of text for numbers, and add them. ie:

Input from text field: "This is my blob of text. Today I went to the store and bought shoes for $50.00 and a sandwich for $7.75."

Output from script: "You spent $57.75!"

Obviously there could be some potential issues. A) what if the user doesn't enter a . to separate dollars and cents? You could come up with $5,775 for the above example.

Looking for some help, the whole / \ ^ 30z;nb 034485 of PHP gets aggravating to someone who is used to C languages clean syntax. I've been using something like the following just to try and find a number, let alone get the various values, sort out dollars/cents and add them. Not to mention this should be able to be done for more than one number in the string.

Code: Select all

 
$message = $HTTP_POST_VARS['message'];
 
preg_match(/ stuffthathastobewrong /, $message, $match);
 
echo "The number in the string is " . $match[1];
 
If anyone could give me some good advice, it would be much appreciated. I'm also looking to buy some good books on PHP. If you guys could recommend some really AWESOME books, that would be great also.


.t2h
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: finding numbers and adding them

Post by prometheuzz »

take2hikes wrote:Alright.. I am reading the posts in this forum regarding reg expressions. I'm not getting anything to really work on my server. I'm a newb to PHP and I'm trying to do the following:

Search a 'blob' of text for numbers, and add them. ie:

Input from text field: "This is my blob of text. Today I went to the store and bought shoes for $50.00 and a sandwich for $7.75."

Output from script: "You spent $57.75!"

Obviously there could be some potential issues. A) what if the user doesn't enter a . to separate dollars and cents? You could come up with $5,775 for the above example.
Well, what else than $5775,- should be the result when the input is "... shoes for $5000 and a sandwich for $775"?
take2hikes wrote:Looking for some help, the whole / \ ^ 30z;nb 034485 of PHP gets aggravating to someone who is used to C languages clean syntax. I've been using something like the following just to try and find a number,
Could you post what you've tried?
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Re: finding numbers and adding them

Post by take2hikes »

Code: Select all

 
<?php
$message = $HTTP_POST_VARS['message'];
 
preg_match("/[a-z\s]+(\d+)[a-z\s]+/i", $message, $match);
echo "The number in the string is " . $match[1];
?>
 
I tried that most recently from some guides I found online. Nothing is too descriptive though, at least with syntax. For the most part they assume you know what all ^, /, + mean.

My only issues are really just understanding the syntax and had to form it. If I get that down finding functions is too simple.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: finding numbers and adding them

Post by prometheuzz »

take2hikes wrote:

Code: Select all

 
<?php
$message = $HTTP_POST_VARS['message'];
 
preg_match("/[a-z\s]+(\d+)[a-z\s]+/i", $message, $match);
echo "The number in the string is " . $match[1];
?>
 
I tried that most recently from some guides I found online. Nothing is too descriptive though, at least with syntax. For the most part they assume you know what all ^, /, + mean.
Okay.
So, don't you know what ^, /, + mean?
I'm not sure what your exact question/problem is at the moment. Did the code you now posted return something you didn't expect?
Perhaps the problem is that you know too little PHP in order to do what you're trying? In that case, all I can do is point you towards a PHP tutorial, because explaining PHP's regex functions to someone not familiar with PHP itself is useless. Or perhaps you know PHP but are interested in learning regex from the basics, in that case, checkout this tutorial: http://www.regular-expressions.info/tutorialcnt.html, which handles all the basic building block of regex.

Good luck.
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Re: finding numbers and adding them

Post by take2hikes »

The first link you posted I've been too. I'll go back through and look again. The code I posted didn't return ANY result. None printed anyways, which wasn't expected.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: finding numbers and adding them

Post by prometheuzz »

take2hikes wrote:The first link you posted I've been too. I'll go back through and look again. The code I posted didn't return ANY result. None printed anyways, which wasn't expected.
Well, then that particular string didn't have digits surrounded by lower case characters or white space characters.

The following:

Code: Select all

<?php
$message = 'abc 123 def';
preg_match("/[a-z\s]+(\d+)[a-z\s]+/i", $message, $match);
echo "The number in the string is " . $match[1];
?>


will print:

Code: Select all

The number in the string is 123
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: finding numbers and adding them

Post by prometheuzz »

Code: Select all

<?php
$message = 'This is my blob of text. Today I went to the store and bought shoes for $50.00 and a sandwich for $7.75.';
preg_match_all("/\d+(\.\d+)?/", $message, $matches);
$sum = 0;
foreach($matches[0] as $value) {
  $sum += $value;
}
echo "sum = $sum";
?>
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Re: finding numbers and adding them

Post by take2hikes »

Thank you, that is a great help. My only question is regarding:

Code: Select all

/\d+(\.\d+)?/
I understand what \d+ does, but can you explain the rest long hand? If I can get that from you I should be good to go on my own, I just need a 'real world' explanation of how all of the metacharacters interact.

Thanks a bunch, sorry if my questions are basic.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: finding numbers and adding them

Post by prometheuzz »

No problem.
I highly recommend reading: http://www.regular-expressions.info/tutorialcnt.html
Post Reply