FILTER_VALIDATE_INT - newbie question
FILTER_VALIDATE_INT - newbie question
am 07.10.2009 11:57:31 von talofo talofo
Hello all,
I'm having this strange behavior, and I do not understanding why...
When I use FILTER_VALIDATE_INT I'm unable to get, on my input box, =
values
starting with 0(zero), or values that are greater (in length) then 10
digits.
I was expecting to be able to input the all range of integers.
I've tried this:
if (!filter_var($telefone, FILTER_VALIDATE_INT))
{
$erros['telefone'] =3D 'Invalid Tel=E9fono - Only Numbers Allowed.';
}
And this:
if (!filter_var($telefone, FILTER_VALIDATE_INT, array("options" =3D>
array("min_range"=3D>-1, "max_range"=3D>9999999999999))))
{
$erros['telefone'] =3D 'Invalid Tel=E9fono - Only Numbers Allowed.';
}
And this:
if (filter_var($telefone, FILTER_VALIDATE_INT, array("options" =3D>
array("min_range"=3D>0, "max_range"=3D>99999999999999999))) == false =
)
{
$erros['telefone'] =3D 'Invalid Tel=E9fono - Only Numbers Allowed.';
}
No success. :s
What am I not getting?
Thanks,
M=E1rcio
Ps- Anyone knows where can we grab a good source information about
FILTER_VALIDATE_INT specifically?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 12:04:07 von Ashley Sheridan
On Wed, 2009-10-07 at 10:57 +0100, MEM wrote:
> Hello all,
>=20
>=20
> I'm having this strange behavior, and I do not understanding why...
>=20
> When I use FILTER_VALIDATE_INT I'm unable to get, on my input box, values
> starting with 0(zero), or values that are greater (in length) then 10
> digits.
>=20
> I was expecting to be able to input the all range of integers.
>=20
>=20
> I've tried this:
>=20
> if (!filter_var($telefone, FILTER_VALIDATE_INT))
> {
> $erros['telefone'] =3D 'Invalid Teléfono - Only Numbers Allowed.'=
;
> }
>=20
> And this:
> if (!filter_var($telefone, FILTER_VALIDATE_INT, array("options" =3D>
> array("min_range"=3D>-1, "max_range"=3D>9999999999999))))
> {
> $erros['telefone'] =3D 'Invalid Teléfono - Only Numbers Allowed.';
> }
>=20
> And this:
> if (filter_var($telefone, FILTER_VALIDATE_INT, array("options" =3D>
> array("min_range"=3D>0, "max_range"=3D>99999999999999999))) == false =
)
> {
> $erros['telefone'] =3D 'Invalid Teléfono - Only Numbers Allowed.';
> }
>=20
> No success. :s
>=20
> What am I not getting?
>=20
>=20
> Thanks,
> Márcio
>=20
>=20
> Ps- Anyone knows where can we grab a good source information about
> FILTER_VALIDATE_INT specifically?
>=20
>=20
Well, at a guess, if a number is 0, PHP see's that as equivalent to a
false. Also, numbers over 10 digits may be going over the limit for your
PHP install, assuming PHP actually attempts to convert the string to a
real integer for the purpose of the filter.
For things this simple, I've always tended to go with something like
this:
if(!preg_match('^[0-9 \+]$', $telefone))
{
$erros['telefone'] =3D 'Invalid Teléfono - Only Numbers Allowed.';
}
Which will check for all common types of phone numbers, including those
where the user has put spaces in to separate groups of digits, and those
that contain the + for country codes.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 12:19:54 von talofo talofo
> Well, at a guess, if a number is 0, PHP see's that as equivalent to a
> false.=20
In this case, shouldn't php ignore the 0 as false?
> Also, numbers over 10 digits may be going over the limit for
> your
> PHP install, assuming PHP actually attempts to convert the string to a
> real integer for the purpose of the filter.
This happens with two different php configurations... Is it common to =
have this kind of limit on php configuration?
Is this something that I need to be aware of?
>=20
> For things this simple,=20
THIS IS NOT SIMPLE !!! :) Newbie blood is in there! :p
Kidding. ;)
> I've always tended to go with something like
> this:
>
> if(!preg_match('^[0-9 \+]$', $telefone))
> {
> $erros['telefone'] =3D 'Invalid Teléfono - Only Numbers =
Allowed.';
> }
>=20
> Which will check for all common types of phone numbers, including =
those
> where the user has put spaces in to separate groups of digits, and
> those
> that contain the + for country codes.
I will absolutely considerer the use of your regex. Thanks a lot.
Before your e-mail, I've considering the use of this:
if (!is_numeric($telefone))
{
$erros['numberofsomething'] =3D 'Error - only numbers please.';
}
In the case we need to validate other ints, that are not phones for =
example, only an unlimited range of ints,
and we want to store that values on a database, should we have special =
considerations regarding the use of is_numeric ?
Thanks a lot,
Márcio
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 12:24:25 von talofo talofo
[sic]
> Before your e-mail, I've considering the use of this:
> if (!is_numeric($telefone))
> {
> $erros['numberofsomething'] = 'Error - only numbers please.';
> }
I meant this of course:
if (!is_numeric($numberofsomething))
{
$erros['numberofsomething'] = 'Error - only numbers please.';
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 12:25:09 von Ashley Sheridan
--=-vLiZp2nskNXbvuXAEo+W
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
On Wed, 2009-10-07 at 11:19 +0100, MEM wrote:
> > Well, at a guess, if a number is 0, PHP see's that as equivalent to a
> > false.=20
>=20
> In this case, shouldn't php ignore the 0 as false?
>=20
>=20
>=20
> > Also, numbers over 10 digits may be going over the limit for
> > your
> > PHP install, assuming PHP actually attempts to convert the string to a
> > real integer for the purpose of the filter.
>=20
> This happens with two different php configurations... Is it common to hav=
e this kind of limit on php configuration?
> Is this something that I need to be aware of?
>=20
>=20
> >=20
> > For things this simple,=20
> THIS IS NOT SIMPLE !!! :) Newbie blood is in there! :p
> Kidding. ;)
>=20
> > I've always tended to go with something like
> > this:
> >
> > if(!preg_match('^[0-9 \+]$', $telefone))
> > {
> > $erros['telefone'] =3D 'Invalid Teléfono - Only Numbers Allowed.=
';
> > }
> >=20
> > Which will check for all common types of phone numbers, including those
> > where the user has put spaces in to separate groups of digits, and
> > those
> > that contain the + for country codes.
>=20
> I will absolutely considerer the use of your regex. Thanks a lot.
>=20
>=20
> Before your e-mail, I've considering the use of this:
> if (!is_numeric($telefone))
> {
> $erros['numberofsomething'] =3D 'Error - only numbers please.';
> }
>=20
> In the case we need to validate other ints, that are not phones for examp=
le, only an unlimited range of ints,
> and we want to store that values on a database, should we have special co=
nsiderations regarding the use of is_numeric ?
>=20
>=20
>=20
> Thanks a lot,
> Márcio
>=20
Well, it was only a guess, but if you look at the integer limit on
32-bit systems, you'll see that the upper limit for numbers is
2147483647 (or 2^31-1) which would explain maybe your upper limit
problem.
Also, I think you're getting confused over the zero with exactly what
you are asking PHP to do. filter_var() returns true if the filter
matches. If the 0 match is returned as a false, then filter_var() will
return false. You're then inverting that with a !, all of which is
inside an if() statement. Essentially this would mean that if the filter
returns false then the instructions inside of the if statement are
carried out.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-vLiZp2nskNXbvuXAEo+W--
RE: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 13:03:24 von talofo talofo
> Well, it was only a guess, but if you look at the integer limit on =
32-bit systems, you'll see that the upper limit > for numbers is =
2147483647 (or 2^31-1) which would explain maybe your upper limit =
problem.
>
> Also, I think you're getting confused over the zero with exactly what =
you are asking PHP to do. filter_var() returns > true if the filter =
matches. If the 0 match is returned as a false, then filter_var() will =
return false. You're then > inverting that with a !, all of which is =
inside an if() statement. Essentially this would mean that if the filter =
> returns false then the instructions inside of the if statement are =
carried out.
I always thought that that was a limit of the number of digits an =
integer value could have, and not the actually int value... I guess I =
was wrong. :s
You are right, I've tested with:=20
2147483647=20
it worked.
I've tested with:
2147483648
It displays the error.
"you're getting confused over the zero with exactly what you are asking =
PHP to do"
Absolutely... :(
If I put 0 filter_var() will return false.
If I put 0342352 filter_var() will also return false.
Could we say that: if it is indeed the fact, that filter_var() returns =
false when it finds a 0 at the beginning of a given number...
"then the instructions inside of the if statement are carried out."
And here may be the reason for displaying the error message when we have =
the 0 leading a number.
And my point was exactly here
"If the 0 match is returned as a false"=20
Why should filter_var() do something like this? Should the filter_var() =
interpretate 0 as a number without boolean semantic value?
Please be patient...=20
Regards,
Márcio
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 13:14:15 von Ashley Sheridan
On Wed, 2009-10-07 at 12:03 +0100, MEM wrote:
> > Well, it was only a guess, but if you look at the integer limit on 32-b=
it systems, you'll see that the upper limit > for numbers is 2147483647 (or=
2^31-1) which would explain maybe your upper limit problem.
> >
> > Also, I think you're getting confused over the zero with exactly what y=
ou are asking PHP to do. filter_var() returns > true if the filter matches.=
If the 0 match is returned as a false, then filter_var() will return false=
.. You're then > inverting that with a !, all of which is inside an if() sta=
tement. Essentially this would mean that if the filter > returns false then=
the instructions inside of the if statement are carried out.
>=20
>=20
> I always thought that that was a limit of the number of digits an integer=
value could have, and not the actually int value... I guess I was wrong. :=
s
>=20
> You are right, I've tested with:=20
> 2147483647=20
> it worked.
>=20
> I've tested with:
> 2147483648
> It displays the error.
>=20
> "you're getting confused over the zero with exactly what you are asking P=
HP to do"
> Absolutely... :(
>=20
> If I put 0 filter_var() will return false.
> If I put 0342352 filter_var() will also return false.
>=20
> Could we say that: if it is indeed the fact, that filter_var() returns fa=
lse when it finds a 0 at the beginning of a given number...
>=20
> "then the instructions inside of the if statement are carried out."
>=20
> And here may be the reason for displaying the error message when we have =
the 0 leading a number.
>=20
> And my point was exactly here
> "If the 0 match is returned as a false"=20
>=20
> Why should filter_var() do something like this? Should the filter_var() i=
nterpretate 0 as a number without boolean semantic value?
>=20
>=20
> Please be patient...=20
> Regards,
> Márcio
>=20
Imho it shouldn't behave like this. 01 is equally as valid as 1 as far
as numbers go, we just ignore leading 0's on any number unless they are
in a significant position (i.e. 0.1)
I am also surprised to see that while it converts the value to an
integer, it doesn't seem to do that first, because 01 is a valid
integer:
$i =3D 0;
$j =3D 01;
var_dump($i);
var_dump($j);
?>
The output returns:
int(0)
int(1)
So the filter seems to be doing some form of check on leading zero's
before checking the validity of the number!
Thanks,
Ash
http://www.ashleysheridan.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 13:46:45 von Tom Worster
On 10/7/09 6:04 AM, "Ashley Sheridan" wrote:
> On Wed, 2009-10-07 at 10:57 +0100, MEM wrote:
>> Hello all,
>>=20
>>=20
>> I'm having this strange behavior, and I do not understanding why...
>>=20
>> When I use FILTER_VALIDATE_INT I'm unable to get, on my input box, value=
s
>> starting with 0(zero), or values that are greater (in length) then 10
>> digits.
>>=20
>> I was expecting to be able to input the all range of integers.
>>=20
>>=20
>> I've tried this:
>>=20
>> if (!filter_var($telefone, FILTER_VALIDATE_INT))
>> {
>> $erros['telefone'] =3D 'Invalid Tel=E9fono - Only Numbers Allowed.';
>> }
>>=20
>> And this:
>> if (!filter_var($telefone, FILTER_VALIDATE_INT, array("options" =3D>
>> array("min_range"=3D>-1, "max_range"=3D>9999999999999))))
>> {
>> $erros['telefone'] =3D 'Invalid Tel=E9fono - Only Numbers Allowed.';
>> }
>>=20
>> And this:
>> if (filter_var($telefone, FILTER_VALIDATE_INT, array("options" =3D>
>> array("min_range"=3D>0, "max_range"=3D>99999999999999999))) == false )
>> {
>> $erros['telefone'] =3D 'Invalid Tel=E9fono - Only Numbers Allowed.';
>> }
>>=20
>> No success. :s
>>=20
>> What am I not getting?
>>=20
>>=20
>> Thanks,
>> M=E1rcio
>>=20
>>=20
>> Ps- Anyone knows where can we grab a good source information about
>> FILTER_VALIDATE_INT specifically?
>>=20
>>=20
>=20
> Well, at a guess, if a number is 0, PHP see's that as equivalent to a
> false. Also, numbers over 10 digits may be going over the limit for your
> PHP install, assuming PHP actually attempts to convert the string to a
> real integer for the purpose of the filter.
isn't a numeric string starting with 0 taken as octal? if so then 8 and 9
might cause validation errors.
> For things this simple, I've always tended to go with something like
> this:
>=20
> if(!preg_match('^[0-9 \+]$', $telefone))
> {
> $erros['telefone'] =3D 'Invalid Tel=E9fono - Only Numbers Allowed.';
> }
>=20
> Which will check for all common types of phone numbers, including those
> where the user has put spaces in to separate groups of digits, and those
> that contain the + for country codes.
right. phone numbers are not integers. people also often use parentheses,
hyphens or dots in them.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 19:13:03 von Ben Dunlap
> Also, I think you're getting confused over the zero with exactly what
> you are asking PHP to do. filter_var() returns true if the filter
> matches. If the 0 match is returned as a false, then filter_var() will
filter_var() actually returns the filtered data if the filter matches,
and FALSE if it doesn't. That's the whole point of the filter_XXX
functions; to pass a tainted value through a filter and get a clean,
"safe" value out the other end:
$tainted = get_user_input();
$clean = filter_var($tainted, [FILTER_CONSTANT]);
// now use $clean and never touch $tainted again
From the original code above, it looks like the OP was
misunderstanding the use of filter_var() and expecting it to return a
boolean.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 19:30:45 von Ben Dunlap
> If I put 0 filter_var() will return false.
Actually it returns the integer 0, not the boolean FALSE. Here's an
illustration of the difference:
http://codepad.org/73wff2u0
The integer value 0 can masquerade as "false" in an if() statement, of
course, as Ash pointed out above.
> If I put 0342352 filter_var() will also return false.
How is 0342352 being assigned to the variable that you're filtering?
If PHP thinks it's a string, then the filter will fail. If PHP thinks
it's a number, it seems to convert it internally to the number 115946,
before you get to the filter. Not sure what's going on there. At any
rate it will then pass FILTER_VALIDATE_INT, but the value's not going
to be what you expect. You can see it happening here:
http://codepad.org/tw2qlpC1
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 19:36:52 von Ben Dunlap
> How is 0342352 being assigned to the variable that you're filtering?
> If PHP thinks it's a string, then the filter will fail. If PHP thinks
> it's a number, it seems to convert it internally to the number 115946,
> before you get to the filter. =A0Not sure what's going on there. At any
Sorry, brain fart. In PHP, a leading 0 in an integer indicates an
octal number (thanks, Martin). PHP immediately converts it to decimal
internally. Hence 0342352 becomes 115946. But it's a bit of a fluke
that the example you used happened to be a valid octal number. Try
something that starts with 0 and has an 8 or a 9 in it; you'll end up
with plain old 0 (presumably because PHP's internal attempt to convert
from octal, fails):
http://codepad.org/KBUgAZWJ
Which, of course, leads to the apparent-false discussed above.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 19:45:50 von Ben Dunlap
> How is 0342352 being assigned to the variable that you're filtering?
> If PHP thinks it's a string, then the filter will fail. If PHP thinks
Oops, potentially bad information there as well, sorry. In general, a
string representation of a decimal number /will/ pass
FILTER_VALIDATE_INT. But your particular string ("0342352") will only
fail FILTER_VALIDATE_INT in the filter's default configuration; set
the ALLOW_OCTAL flag and it will pass:
http://codepad.org/RNE5LZMr
You'll still end up with an unexpected value in your final variable, though.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: FILTER_VALIDATE_INT - newbie question
am 07.10.2009 22:57:12 von Martin Scotta
--000325559f3e53721404755e9747
Content-Type: text/plain; charset=ISO-8859-1
Are you using the == operator or === ?
maybe that's the problem
the function could return false or 0. You need to use the === operator
if( false !== ( $value = filter_var($value, FILTER_VALIDATE_INT)))
{
echo 'I need an Integer';
exit;
}
echo 'Thanks for the Int';
On Wed, Oct 7, 2009 at 2:13 PM, Ben Dunlap wrote:
> > Also, I think you're getting confused over the zero with exactly what
> > you are asking PHP to do. filter_var() returns true if the filter
> > matches. If the 0 match is returned as a false, then filter_var() will
>
> filter_var() actually returns the filtered data if the filter matches,
> and FALSE if it doesn't. That's the whole point of the filter_XXX
> functions; to pass a tainted value through a filter and get a clean,
> "safe" value out the other end:
>
> $tainted = get_user_input();
> $clean = filter_var($tainted, [FILTER_CONSTANT]);
> // now use $clean and never touch $tainted again
>
> From the original code above, it looks like the OP was
> misunderstanding the use of filter_var() and expecting it to return a
> boolean.
>
> Ben
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Martin Scotta
--000325559f3e53721404755e9747--
RE: FILTER_VALIDATE_INT - newbie question
am 08.10.2009 01:27:45 von talofo talofo
Thank you all for your replies.=20
The issue was "solved" for a better fitting solution on this case, that =
was
by using a regex expression.
I've not tested the difference between using == or ===3D. I was =
using !
But, the main question that is not clear on my newbie head is, "why how =
why"
couldn't this 0 be interpreted, neither as octal, or false, or whatever =
it
may be, but as just a humble numeric value of 0. At least on the cases =
where
FILTER_VALIDADE_INT is there.=20
Anyway, I cannot dig in into this because I'm actually unable to offer =
some
solutions.=20
So, as stated, the issue was solved, by using a regex, so all good here. =
;)
Thanks once again,
M=E1rcio
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php