Jarod Posted February 14, 2010 Share Posted February 14, 2010 Well I give up on this... Even though I learned a lot about regex, but forget it... Anyways, I've been trying to figure out how I would handle my inputs so that it only looks at the numbers and nothings else. Check out 900,000,000,000 for example (900 billions btw). I enter it exactly as, but when it comes down for processing it I only want the numbers to go through, which would be 900000000000. I've heard of regex before, but never actually understood its purpose and how its suppose to be used. So anyways I used the expressions /[0-9]{1,3}[^,]*/, but the problem of that is I cant get it to only look at the numbers (omitting the commas and only using the numbers). Its array would output Array [0] ( $match[0] = 900; $match[1] = 000; $match[2] = 000; $match[3] = 000; ) Any way I can get it to look at the string like this??? (900000000000) Quote Link to comment https://forums.phpfreaks.com/topic/192010-i-want-to-get-the-numbers-only-and-omit-the-commas/ Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 You could use more basic string functions instead, for example: str_replace(',', '', '900,000,000,000') If you want to use a regular expression, the following simply removes all commas: preg_replace('/,/', '', '900,000,000,000') Quote Link to comment https://forums.phpfreaks.com/topic/192010-i-want-to-get-the-numbers-only-and-omit-the-commas/#findComment-1012039 Share on other sites More sharing options...
JAY6390 Posted February 14, 2010 Share Posted February 14, 2010 Or better still use $string = preg_replace('/[^\d\.]+/', '', $string); Which will remove anything but numbers and decimal points. It's ideal for strings for currency like $54.09 and so on Quote Link to comment https://forums.phpfreaks.com/topic/192010-i-want-to-get-the-numbers-only-and-omit-the-commas/#findComment-1012109 Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 Or better still use $string = preg_replace('/\D/', '', $string); Which will remove anything but numbers. It's ideal for strings for numbers like 900,000,000,000 and so on [ot]Sorry. [/ot] Quote Link to comment https://forums.phpfreaks.com/topic/192010-i-want-to-get-the-numbers-only-and-omit-the-commas/#findComment-1012114 Share on other sites More sharing options...
JAY6390 Posted February 14, 2010 Share Posted February 14, 2010 Yup, but can only handle integers Quote Link to comment https://forums.phpfreaks.com/topic/192010-i-want-to-get-the-numbers-only-and-omit-the-commas/#findComment-1012119 Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 The OP never gave any hint that non-integer numbers need to be catered for. Then again, they never gave any hint that anything other than commas need to be dealt with. Quote Link to comment https://forums.phpfreaks.com/topic/192010-i-want-to-get-the-numbers-only-and-omit-the-commas/#findComment-1012123 Share on other sites More sharing options...
JAY6390 Posted February 14, 2010 Share Posted February 14, 2010 lol very true Quote Link to comment https://forums.phpfreaks.com/topic/192010-i-want-to-get-the-numbers-only-and-omit-the-commas/#findComment-1012128 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.