note 78315 modified in function.money-format by danbrown

From: Date: Tue, 16 Jun 2009 16:01:43 +0000
Subject: note 78315 modified in function.money-format by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156928@lists.php.net to get a copy of this message
This is a handy little bit of code I just wrote, as I was not able to find anything else suitable for my situation. This will handle monetary values that are passed to the script by a user, to reformat any comma use so that it is not broken when it passes through an input validation system that checks for a float. It is not foolproof, but will handle the common input as most users would input it, such as 1,234,567 (outputs 1234567) or 1,234.00 (outputs 1234.00), even handles 12,34 (outputs 12.34), I expect it would work with negative numbers, but have not tested it, as it is not used for that in my situation. This worked when other options such as money_format() were not suitable or possible. <?php /////////////// // BEGIN CODE convert all price amounts into well formatted values function converttonum($convertnum,$fieldinput){ $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); } $_POST[$fieldinput] = $convertnum; // redefine the vlaue of the variable, to be the new corrected one } @converttonum($_POST[inputone],"inputone"); @converttonum($_POST[inputtwo],"inputtwo"); @converttonum($_POST[inputthree],"inputthree"); // END CODE ////////////// ?> This is suitable for the English usage, it may need tweaking to work with other types. --was-- This is a handy little bit of code I just wrote, as I was not able to find anything else suitable for my situation. This will handle monetary values that are passed to the script by a user, to reformat any comma use so that it is not broken when it passes through an input validation system that checks for a float. It is not foolproof, but will handle the common input as most users would input it, such as 1,234,567 (outputs 1234567) or 1,234.00 (outputs 1234.00), even handles 12,34 (outputs 12.34), I expect it would work with negative numbers, but have not tested it, as it is not used for that in my situation. This worked when other options such as money_format() were not suitable or possible. =============== /////////////// // BEGIN CODE convert all price amounts into well formatted values function converttonum($convertnum,$fieldinput){ $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); } $_POST[$fieldinput] = $convertnum; // redefine the vlaue of the variable, to be the new corrected one } @converttonum($_POST[inputone],"inputone"); @converttonum($_POST[inputtwo],"inputtwo"); @converttonum($_POST[inputthree],"inputthree"); // END CODE ////////////// ================ This is suitable for the English usage, it may need tweaking to work with other types. http://php.net/manual/en/function.money-format.php

« previous php.notes (#156928) next »