note 75305 deleted from function.eregi by tularis
| From: | tularis@php.net | Date: | Fri, 25 May 2007 23:04:56 +0000 |
| Subject: | note 75305 deleted from function.eregi by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126924@lists.php.net to get a copy of this message | ||
Note Submitter: peter at sharphorizon dot com
Reason: bad code
----
If you don't want to use a regular expression to match values in a string, especially a
currency formatted string, then you can always decompile the string and make individual checks.
This function checks the formatting of a number so that it matches 0.00 -> 00000.00 (increasing
leading numbers). There must always be a decimal point and the accuracy must always be two decimal
places.
// Process:
// 1 - Separate and check the suffix for numeric values only
// 2 - Find the decimal point in the correct place
// 3 - Check the first decimal for numeric values only
// 4 - Check the second decimal for numeric values only
// Returns TRUE if all criteria are met ($success=="4")
// Returns FALSE if any criteria are not met ($success<"4")
function checkcurrency($inp){
$len=strlen($inp);
$trimmer=0;
$success=0; // tally of met criteria
$fail=0; // set to 1 if a character is found
$end=$len-3; // the end position of the start
$trimmed="";
while($trimmer<$end){
$trimmed=$trimmed.$inp[$trimmer];
$foo=$inp[$trimmer];
if(!eregi('[0-9]', $foo)){
$fail+=1; // not number = failure
}
$trimmer+=1;
}
if($fail==0){
$success+=1;
}
$twoback=$len-3; // must be a .
$dotFinder=$inp[$twoback];
if($dotFinder=="."){
$success+=1;
}
$lastchar=$len-1; // last position
$oneback=$len-2; // second last position
$last=$inp[$lastchar];
if(eregi('[0-9]', $last)){
$success+=1;
}
$secondlast=$inp[$oneback];
if(eregi('[0-9]', $secondlast)){
$success+=1;
}
if($success==4){
return true;
} else {
return false;
}
}