Author | Thread |
|
03/29/2012 06:10:49 PM · #1 |
Need to convert a string into an integer. Example is $150,000 => 150000
Googled for the answer, but got mostly the other way around (integer to currency). I'm sure this should be simple. Thanks in advance. |
|
|
03/29/2012 06:17:16 PM · #2 |
$str = preg_replace('/[^0-9]/s', '', $str);
|
|
|
03/29/2012 06:21:14 PM · #3 |
ok. *slaps forehead* duh. THANKS! |
|
|
03/29/2012 06:23:29 PM · #4 |
Originally posted by james_so: $str = preg_replace('/[^0-9]/s', '', $str); |
I don't think that will work with decimals? e.g. $150,000.99 |
|
|
03/29/2012 06:29:09 PM · #5 |
Eh, maybe not, but I don't need to worry about decimals in this instance. It worked fine. Good point, though, bvy. |
|
|
03/29/2012 06:31:02 PM · #6 |
If you need decimals do a \.* and a \d* afterwards, I'd say.
Edit: But you don't so you're fine
Message edited by author 2012-03-29 18:31:14.
|
|
|
03/29/2012 06:44:21 PM · #7 |
Originally posted by NiallOTuama: If you need decimals do a \.* and a \d* afterwards, I'd say.
Edit: But you don't so you're fine |
Still good to know. So how would that look in the code? |
|
|
03/29/2012 06:50:26 PM · #8 |
Sorry. Wasn't paying attention, so what I suggested was wrong.
I'd split on "." (might be a "\.", can't remember if split takes regex), and handle the $'s and c's separately. Use the preg_replace as per byv on the first result and if it splits you have the second.
Message edited by author 2012-03-29 18:51:26.
|
|
|
03/29/2012 07:17:41 PM · #9 |
|
Home -
Challenges -
Community -
League -
Photos -
Cameras -
Lenses -
Learn -
Help -
Terms of Use -
Privacy -
Top ^
DPChallenge, and website content and design, Copyright © 2001-2025 Challenging Technologies, LLC.
All digital photo copyrights belong to the photographers and may not be used without permission.
Current Server Time: 08/14/2025 03:48:27 AM EDT.