RE: [PHP] Extracting Numbers from a string.
OK y'all, go ahead and shoot me, but this is what I'd do: $mystring=enters $56.55 for example; $mynumber=$mystring+1-1; $mynumber now equals 56.55 If you want the . to go to, use the other methods. -Original Message- From: Jason Caldwell [mailto:[EMAIL PROTECTED]] Sent: Wednesday, September 11, 2002 3:31 PM To: [EMAIL PROTECTED] Subject: [PHP] Extracting Numbers from a string. I need to extract the numbers only from a field. For example: I have an AMOUNT field and so that I can filter out any user typo's I would like to extract the numbers only. If the user enters $56.55 for example or just $56 then I would like to be able to remove the $ and the . keeping just the 56 or 5655. Can I use eregi_replace() to do this -- I've been trying but it doesn't seem to work right. Thanks. Jason -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Extracting Numbers from a string.
A simple: eregi_replace([^0-9], , $myvar); should do it. it says.. replace everything that is not a number ( character class [^0-9] which means Character class [ NOT ^ of Decimal 0-9 end character class ] ) with a NULL string, thereby ripping out all non-numeric components and leaving you with a totally numeric string. ( in theory ). I haven't tested the code so you may have to play with it, but it's pretty simple. dan On Wed, 11 Sep 2002 15:31:22 -0700 Jason Caldwell [EMAIL PROTECTED] wrote: I need to extract the numbers only from a field. For example: I have an AMOUNT field and so that I can filter out any user typo's I would like to extract the numbers only. If the user enters $56.55 for example or just $56 then I would like to be able to remove the $ and the . keeping just the 56 or 5655. Can I use eregi_replace() to do this -- I've been trying but it doesn't seem to work right. Thanks. Jason -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Extracting Numbers from a string.
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On September 11, 2002 16:31, Jason Caldwell wrote: I need to extract the numbers only from a field. For example: I have an AMOUNT field and so that I can filter out any user typo's I would like to extract the numbers only. If the user enters $56.55 for example or just $56 then I would like to be able to remove the $ and the . keeping just the 56 or 5655. Can I use eregi_replace() to do this -- I've been trying but it doesn't seem to work right. Hey Jason, You can strip out all non-numeric values with this regular expression: $v = ereg_replace('[^0-9]', '', $v); If you want to keep the decimal place, then use this $v = ereg_replace('[^.0-9]', '', $v); And finally, if you want a regular expression that recognizes monetary values, try this :) $regex = '^' # match the start of a string . '(' # capture any text that matches the regex . '[^0-9]*' # match any character that is not 0-9 . ')' # stop capturing text # Capture match so that we can see if # they used a currency symbol :) . '?' # Optionally match the preceding bracketed text . '(' # capture any text that matches the regex . '[0-9]+' # match an integer number .'|[,.][0-9]{0,2}'# or match a decimal value .'|[0-9]+[,.][0-9]{0,2}' # match integer and decimal .')'# Stop capturing text .'([^0-9]+)'# Capture any trailing text (like USD or CAD) . '?'; # Optionally match the preceding bracketed text if( ereg($regex, $possible_numeric_value, $captured_text) ) { echo $possible_numeric_value might be a monetary value.; var_dump ($captured_text); } else { echo $possible_numeric_value is probably not a monetary value.; } Cheers! - --zak Cheers! -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9f89Cb6QONwsK8bIRAsP4AJ9X6RqWUDJYtagcJvZ37f/UoRJaYgCfbg1S 22ioG6nALcALqXd6xjGxOrQ= =IbP1 -END PGP SIGNATURE- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php