HDFilmMaker2112 Posted June 27, 2011 Share Posted June 27, 2011 I'm looking to know if preg_replace would be the best option to format a users input of a money amount? With multiple different types of entries, it seems it may not work. IE: The four currency formats I know of: 1,200.94 1,200 1.200,94 1.200 How would I be able to replace the decimals and commas, without messing up their positioning? I want to end up with a number formatted in the 1,200.94 format. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 27, 2011 Author Share Posted June 27, 2011 I came up with this, based on something I copied from php.net. The only issue I have is when I enter a value of 1,234,567 it prints out twice. Like this: 12345671234567 <?php $convertnum="1,123,234.86"; $bits = explode(",",$convertnum); // split input value up to allow checking $first = strlen($bits[0]); // gets part before first comma (thousands/millions) $last = strlen($bits[1]); // gets part after first comma (thousands (or decimals if incorrectly used by user) if ($last <3){ // checks for comma being used as decimal place $convertnum = str_replace(",",".",$convertnum); } else{ // assume comma is a thousands seperator, so remove it $convertnum = str_replace(",","",$convertnum); } $thousands = explode(".",$convertnum); $period_count = count($thousands); if($period_count > 1){ $convertnum=str_replace(".",",",$thousands[0]); } for($i=1; $i < $period_count-1; $i++){ $convertnum.="".$thousands[$i]; } $cent_count=strlen($thousands[$period_count-1]); if($cent_count < 3 && $period_count==$period_count){ $convertnum.=".".$thousands[$period_count-1]; } else{ $convertnum.=$thousands[$period_count-1]; } echo $convertnum; // redefine the value of the variable, to be the new corrected one ?> Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted June 27, 2011 Share Posted June 27, 2011 <?php $number = 1234.56; // let's print the international format for the en_US locale setlocale(LC_MONETARY, 'en_US'); echo money_format('%i', $number) . "\n"; // USD 1,234.56 ?> http://php.net/manual/en/function.money-format.php Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 27, 2011 Share Posted June 27, 2011 If you want your result to be usable by a computer as a number (stored, searched, compared, sorted, math operations), you don't want the thousands comma separator in it. You would only want - 12345.67 You would only put in the thousands separator when displaying the number. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 27, 2011 Author Share Posted June 27, 2011 <?php $number = 1234.56; // let's print the international format for the en_US locale setlocale(LC_MONETARY, 'en_US'); echo money_format('%i', $number) . "\n"; // USD 1,234.56 ?> http://php.net/manual/en/function.money-format.php This doesn't work when you want to format data entered in different formats. It won't accept a number with the comma as a decimal separator. I need to be able to accept various inputs: 1234.56 1234 1,234.56 1,234 1.234,56 1.234 and have them all enter into the database as 1234.56 Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted June 27, 2011 Share Posted June 27, 2011 Not sure but I think you should not be accepting too many various inputs, but tell the users how they should input the value instead. I mean what if you create a script to handle comma as a thousand separator and dot as a decimal separator. Or just the opposite, dot as a thousand separator and comma as a decimal separator. Youl you end up having a mess with your values. I think you gotta create certain rules how the input must be made, and then validate it based on that. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 27, 2011 Share Posted June 27, 2011 If it were me and I expected input from people who use different formats, I'd probably use two form fields, one for the whole currency amount and one for the fractional amount. Amount: <input type="text" name="dollars"> . <input type="text" name="cents" size="2" maxlength="2"> Then you can just strip any non-numeric characters from the first field, validate both fields with ctype_digit() and concatenate them with a decimal point to get the final value. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 27, 2011 Author Share Posted June 27, 2011 I came up with this... anybody see any possible flaws? The only thing I can think of is if somebody enters decimal of more than 2 digits. <?php $number ="1.234"; $bits = explode(",",$number); // split input value up to allow checking $last = strlen($bits[1]); // gets part after first comma (thousands (or decimals if incorrectly used by user) if ($last < 3){ if ($last==0){ $number.=",00"; } $number = str_replace(".","",$number); $number = str_replace(",",".",$number); } $number = str_replace(",","",$number); $dec_bits = explode(".",$number); // split input value up to allow checking $dec = strlen($dec_bits[1]); // gets part first decimal point if ($dec==0){ $number.=".00"; } ?> If it were me and I expected input from people who use different formats, I'd probably use two form fields, one for the whole currency amount and one for the fractional amount. Amount: <input type="text" name="dollars"> . <input type="text" name="cents" size="2" maxlength="2"> Then you can just strip any non-numeric characters from the first field, validate both fields with ctype_digit() and concatenate them with a decimal point to get the final value. This maybe a better option. There is additional checking on it... if the value they enter doesn't match the value in the database, then an error is shown... so that should catch any three digits after the decimal point. I just wanted to accept a wider range of possible inputs to make it easier for users. Quote Link to comment 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.