----- Original Message ----- From: "Beth Gore" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, December 02, 2002 9:12 PM Subject: [PHP] Validating get and post data
> Okay, I've just solved my own problem by simply doing: > > settype($input,"integer"); > > but.. I'm puzzled about why the following more complicated solution > didn't work. The ASCII value for 0 is 48, and for 9 is 57. > > The idea was to read a character at a time from the $rawinput string, > check if it's within the correct range, and if so concantenate it to the > end of the output string. > > However bizarrely this seems to behave incorrectly, as it cuts out "0" > as well. Can anyone explain why it does this? > > function stripnum($rawinput) > { > > for($x=0;$x < strlen($rawinput);$x++) > { > $c = substr($rawinput,$x,1); > > switch($c){ > > case ($c > chr(47) and $c < chr(58)): > $output .=$c; > break; I think what's happening here is a type issue. The comparison is returning a boolean, so when $c != '0', the switch is true and the case is resolving to true, and executing. But when $c == '0', with switch is (false), but the case is true. Change the case to ($c > chr(58) and $c < chr(58)): pass in '09090' and see what you get. Perhaps the case isn't the right structure for this test. > > default: > echo "escaped character at ".$x; > break; > } > > } > > return $output; > } > > > I just can't find the bug in my code at all, and even though I found a > better way to do it, it's annoying me that this didn't work!!! > > Beth Gore > -- > http://bethanoia.dyndns.org/ > rss feed: http://bethanoia.dyndns.org/bethanoia.rss > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php