note 75305 added to function.eregi

From: Date: Wed, 23 May 2007 10:16:19 +0000
Subject: note 75305 added to function.eregi
Groups: php.notes 
Request: Send a blank email to php-notes+get-126838@lists.php.net to get a copy of this message
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; } } ---- Server IP: 194.145.210.3 Probable Submitter: 86.150.137.47 ---- Manual Page -- http://www.php.net/manual/en/function.eregi.php Edit -- https://master.php.net/note/edit/75305 Del: integrated -- https://master.php.net/note/delete/75305/integrated Del: useless -- https://master.php.net/note/delete/75305/useless Del: bad code -- https://master.php.net/note/delete/75305/bad+code Del: spam -- https://master.php.net/note/delete/75305/spam Del: non-english -- https://master.php.net/note/delete/75305/non-english Del: in docs -- https://master.php.net/note/delete/75305/in+docs Del: other reasons-- https://master.php.net/note/delete/75305 Reject -- https://master.php.net/note/reject/75305 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126838) next »