note 78309 deleted from function.strlen by nlopess

From: Date: Sat, 06 Oct 2007 10:16:45 +0000
Subject: note 78309 deleted from function.strlen by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-131173@lists.php.net to get a copy of this message
Note Submitter: Scott from ezyauctionz.co.nz ---- 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. =============== /////////////// // 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.

« previous php.notes (#131173) next »