note 14868 added to function.is-numeric.php
| From: | max at uibk dot ac dot at | Date: | Sat, 18 Aug 2001 08:39:19 +0000 |
| Subject: | note 14868 added to function.is-numeric.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-16866@lists.php.net to get a copy of this message | ||
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;
}
}
?>
http://www.php.net/manual/en/function.is-numeric.php