max@uibk.ac.at
18-Aug-2001 03:39
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;
}
}
?>