Jump to content

preg_replace to format money


HDFilmMaker2112

Recommended Posts

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.

Link to comment
Share on other sites

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 

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.