Hi,
Tuesday, December 3, 2002, 5:17:48 PM, you wrote:
TR> Hi,
TR> Tuesday, December 3, 2002, 12:12:09 PM, you wrote:
BG>> Okay, I've just solved my own problem by simply doing:
BG>> settype($input,"integer");
BG>> but.. I'm puzzled about why the following more complicated solution
BG>> didn't work. The ASCII value for 0 is 48, and for 9 is 57.
BG>> The idea was to read a character at a time from the $rawinput string,
BG>> check if it's within the correct range, and if so concantenate it to the
BG>> end of the output string.
BG>> However bizarrely this seems to behave incorrectly, as it cuts out "0"
BG>> as well. Can anyone explain why it does this?
BG>> function stripnum($rawinput)
BG>> {
BG>> for($x=0;$x < strlen($rawinput);$x++)
BG>> {
BG>> $c = substr($rawinput,$x,1);
BG>> switch($c){
BG>> case ($c > chr(47) and $c < chr(58)):
BG>> $output .=$c;
BG>> break;
BG>> default:
BG>> echo "escaped character at ".$x;
BG>> break;
BG>> }
BG>> }
BG>> return $output;
BG>> }
BG>> I just can't find the bug in my code at all, and even though I found a
BG>> better way to do it, it's annoying me that this didn't work!!!
BG>> Beth Gore
BG>> --
BG>> http://bethanoia.dyndns.org/
BG>> rss feed: http://bethanoia.dyndns.org/bethanoia.rss
TR> switch will treat 0 as false which ever way you try to dress it up :)
TR> I solved a similar problem like this:
TR> switch($c){
TR> case (ord($c) > 47 && ord($c) < 58)?True:False:
TR> $output .=$c;
TR> break;
TR> default:
TR> echo "escaped character at ".$x;
TR> break;
TR> }
TR> --
TR> regards,
TR> Tom
BTW you may find this runs a bit quicker
function stripnum($rawinput)
{
$len = strlen($rawinput);
$output = '';
for($x=0;$x < $len;$x++)
{
switch($rawinput[$x]){
case (ord($rawinput[$x]) > 47 && ord($rawinput[$x]) <
58)?True:False:
$output .= $rawinput[$x];
break;
default:
echo "escaped character at ".$x;
break;
}
}
return $output;
}
--
regards,
Tom
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php