note 14868 deleted from function.is-numeric by didou

From: Date: Fri, 03 Oct 2003 11:27:00 +0000
Subject: note 14868 deleted from function.is-numeric by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57705@lists.php.net to get a copy of this message
Note Submitter: max@uibk.ac.at ---- hi ! i've been trying to convert a string passed by a web-form into a double/int but any of the PHP built-ins hasn't worked for me. really dunno why. this is how i have helped myself with a number-verify function: <? // there is a function called "is_numeric" in PHP, so i call this one // "is_it_number" // one probably does not have to initialize the variables like i do, but // i'm used to PL/SQL requirements so ... // this function will allow the tested string to include only // digits and only one . (dot) or , (comma). neither of both // can be a trailing character of the string. // please note that we do not check here if the string is an empty string // function is_it_number($str_in) { $c1 = 0; $c2 = 0; for ($i = 0; $i < strlen($str_in); $i++) { if (preg_match("/[0-9]|[.,]/", substr($str_in,$i,1))) { ;; } else { $c1++; } if (preg_match("/[.,]/", substr($str_in,$i,1))) { $c2++; } } if ($c1 > 0 or $c2 > 1 or preg_match("/[.,]$|[ ]+/", $str_in)) { return (boolean) FALSE; } else { return (boolean) TRUE; } } ?>

« previous php.notes (#57705) next »