Re: Validating get and post data
| From: | Matt | Date: | Tue, 03 Dec 2002 03:30:11 +0000 |
| Subject: | Re: Validating get and post data | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-126802@lists.php.net to get a copy of this message | ||
----- Original Message -----
From: "Beth Gore" <bethgore@tesco.net>
To: <php-general@lists.php.net>
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
>
>
>