Posted by GarryJones on 09/28/07 13:50
The following function checks to see if a variable read from a mysql
database is numeric. The funtion worked until I hit the value 15 303
That is a valid number but because of the space between the 15 and the
303 for (fifteen thousand three hundred and three) it errored
function is_number($number)
{
$text = (string)$number;
$textlen = strlen($text);
if ($textlen==0) return 0;
for ($i=0;$i < $textlen;$i++)
{ $ch = ord($text{$i});
if ( ($ch < 48 ) || ($ch > 57) ) return 0;
}
return 1;
}
So I replaced the second if row with this one to check for character
32 (space) when less then charcter48
if ( (($ch < 48 ) && ($ch != 32)) || ($ch > 57) ) return 0;
But that has not really helped It now gives me this error for 15 303
-
Parse error: syntax error, unexpected T_LNUMBER in...(file) on line
104
Line 100-104 in my test are
100 print is_number(545);
101 print is_number(ca100);
102 print is_number( );
103 print is_number(none);
104 print is_number(15 303);
I also need this to identify (ca 100) as being non-numeric it works as
above on (ca100) but not with the added space between ca and 100.
However line 102 has several spaces and that does not error but gives
"Warning: Missing argument for is_number(), called in (file) on line
102 "
In gathering future input from users is it possible to restrict the
user from being able to type in any other characters than 0-9? I still
need to solve the number check as I am using this code to add one to
the value if conditions are met.
Any help very much appreciated
Garry Jones
Sweden
[Back to original message]
|