note 78311 added to function.strlen

From: Date: Sat, 06 Oct 2007 04:41:19 +0000
Subject: note 78311 added to function.strlen
Groups: php.notes 
Request: Send a blank email to php-notes+get-131163@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. =============== /////////////// // 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. ---- Server IP: 208.69.120.35 Probable Submitter: 202.89.142.46 ---- Manual Page -- http://www.php.net/manual/en/function.strlen.php Edit -- https://master.php.net/note/edit/78311 Del: integrated -- https://master.php.net/note/delete/78311/integrated Del: useless -- https://master.php.net/note/delete/78311/useless Del: bad code -- https://master.php.net/note/delete/78311/bad+code Del: spam -- https://master.php.net/note/delete/78311/spam Del: non-english -- https://master.php.net/note/delete/78311/non-english Del: in docs -- https://master.php.net/note/delete/78311/in+docs Del: other reasons-- https://master.php.net/note/delete/78311 Reject -- https://master.php.net/note/reject/78311 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131163) next »