You are here: Re: Nunber check Function failing « PHP Programming Language « IT news, forums, messages
Re: Nunber check Function failing

Posted by Tyno Gendo on 09/28/07 13:58

GarryJones wrote:
> 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
>

you shouldn't need to write a function to check if something is a
number, use the built in PHP commands, or use regex for validation
if you want eg.

if ( ctype_digit( "100" ) ) {
echo "It's numerical, hallelujah";
} else {
echo "The devil is in our midst, NaN...";
}


However, what you have there really isn't a number "15 303", you would
need to replace any spaces or other chars with str_replace/preg_replace
prior to a number check, you should validate data first before it gets
into your database.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация